Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to "Mock" Final Classes With Mockito

DZone's Guide to

How to "Mock" Final Classes With Mockito

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

 If you are using mockito, then you cannot "mock" final classes. But there is a nice library called JDave which does some bytecode, i.e. CGLIB, magic to change final classes to non-final classes.

Just download JDave, add the JAR files to the classpath, and start your test cases with the following VM option:

-javaagent:your-library-directory\jdave-unfinalizer-1.1.jar

 

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}