DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Mockito 2.x Over PowerMock Migration: Top 10 Tips and Tricks

Mockito 2.x Over PowerMock Migration: Top 10 Tips and Tricks

With the long-awaited release of Mockito 2, we take a look at some tips to help smooth out your migration from 1.x to 2. You may need them.

Hazem Saleh user avatar by
Hazem Saleh
·
Apr. 19, 17 · Tutorial
Like (10)
Save
Tweet
Share
21.56K Views

Join the DZone community and get the full member experience.

Join For Free

after so many years of hopeless waiting, mockito 2.x has been released to solve many problems that developers have had with their tests.

there are great features of mockito 2.x, such as:

  1. finally mocking final classes and methods.
  2. support for java 8.
  3. migration from cglib to bytebuddy.

but if you are having large tests written in mockito 1.x, will it be an easy task to migrate?

image title

unfortunately, the migration most probably will be a painful task because mockito 2.x does not respect the old behavior of mockito 1.x. adding to this complexity, if you use powermock in your old tests, then you will have to face another dimension of complexity, as most of powermock’s versions have integration issues with mockito 2.x.

regarding powermock’s early issues with mockito 2.x, the powermock team announced that powermock 1.6.5 has experimental support for mockito 2.x but unfortunately, it was not that great.

in the beginning, when changing the mockito version to 2.x in your build.gradle file, you may find that more than 50% of your tests were failing: null pointer exceptions, compilation errors, no class definition found, unexpected thrown exception, …etc, and this is how you might look in the beginning of the migration.

image title

do not panic and do not be sad, this artivle mentions some of the important challenges that you may face during the migration and tips to overcome these challenges to save your time.

1. use the proper powermock’s mockito api extension

using the powermock-api-mockito extension does not work with mockito 2.x. you will have the following exception when running your unit tests if you stick to the old extension:

java.lang.noclassdeffounderror: org/mockito/cglib/proxy/methodinterceptor
at org.powermock.api.mockito.internal.mockmaker.powermockmaker.<init>(powermockmaker.java:43)
...
caused by: java.lang.classnotfoundexception: org.mockito.cglib.proxy.methodinterceptor
at java.net.urlclassloader.findclass(urlclassloader.java:381)
at java.lang.classloader.loadclass(classloader.java:424)
at sun.misc.launcher$appclassloader.loadclass(launcher.java:331)
at java.lang.classloader.loadclass(classloader.java:357)
... 40 more


in order to fix this issue, you should use the correct mockito api extension, which is:
powermock-api-mockito2 .

2. avoid using incompatible versions of mockito and powermock

always make sure to use compatible versions of mockito and powermock. for example, the following two versions are compatible:

  • powermock version 1.7.0 rc2.
  • mockito version 2.1.0.

3. say goodbye to mockito’s whitebox

mockito 2.x does not have whitebox anymore.

so what is the solution then?

initially, you can use powermock’s whitebox instead of the removed mockito 2.x whitebox. however, you need to know that this does not come without problems, such as this one that i reported to powermock:

org.powermock.reflect.exceptions.fieldnotfoundexception: no instance field
named xxx could be found in the class hierarchy

so if the initial solution does not work for you, consider writing your own. it's really not that hard.

4. using the right matchers

never forget to always use org.mockito.argumentmatchers instead of the old org.mockito.matchers.

for example, replace the old matcher imports:

import static org.mockito.matchers.any;
import static org.mockito.matchers.anyboolean;
import static org.mockito.matchers.anyint;
import static org.mockito.matchers.anystring;
import static org.mockito.matchers.eq;


with the following ones:

import static org.mockito.argumentmatchers.any;
import static org.mockito.argumentmatchers.anyboolean;
import static org.mockito.argumentmatchers.anyint;
import static org.mockito.argumentmatchers.anystring;
import static org.mockito.argumentmatchers.eq;


and so on with other matchers.

5. anyint() does not match long literals anymore

after the upgrade, you may find anyint() does not work because it cannot match long literals, such as 0l for example.

so in order to fix this issue, just replace anyint() with anylong() and you will be fine.

note : if you can match with the actual value instead of anyxxx(), this will be much better, as your test will have more transparency.

6. anystring() does not match null anymore

this can make a lot of tests fail, but anystring() does not include null anymore in mockito 2.x. this applies also to any(xxx). for example, any(inputstream.class) does not match null in mockito 2.x.

note : if you can match with the actual value instead of anyxxx(), this will be much better, as your test will have more transparency.

7. mockito 2.7.1 is not a real friend to powermock

unfortunately, if you use powermock 1.6.5 or even powermock 1.7.0rc2 with mockito 2.7.1 (the latest version at the time of writing this post), you will find the following exception with donothing :

java.lang.illegalaccesserror: 
tried to access method 
org.mockito.internal.stubbing.answers.doesnothing.<init>()v from 
class org.powermock.api.mockito.powermockito


fortunately, this issue is fixed with powermock 1.7.0rc4 . here's more information .

so in summary, if you use mockito 2.7.1, do not forget to use powermock 1.7.0rc4.

8. original test exceptions are wrapped as runtimeexceptionproxy in mockito 2.x with powermock

unfortunately, as a workaround, you have to modify all the broken @test(expected=someexception.class) to @test(expected=exception.class) since the original exceptions are wrapped as mockito runtimeexceptionproxy in mockito 2.x with powermock.

this issue really requires further investigation to know why mockito 2.x does this wrapping with powermock. if you have a better solution for this, feel free to comment.

9. move away from powermock and depend on mockito 2.x only

try to create a plan to remove powermock by refactoring your app classes to be testable. mockito 2.x is really enough now.

10. review old tests

take this migration as a chance to review the old tests and to improve them in order to have more maintainable tests. this can help you strengthen your product code and allow easier refactoring for the current code base without surprises.

Mockito unit test

Published at DZone with permission of Hazem Saleh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Multi-Cloud Integration
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • 11 Observability Tools You Should Know
  • Reliability Is Slowing You Down

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: