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

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

 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

 

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}