Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Powemockito for static mocking

I would like to make use of Powermock with Mockito to mock some static method calls. I have followed instructions and examples from SO as well as the PowerMock Getting Started and MockStatic pages as best I can, but I am yet to complete a mockStatic() call.

When I call mockStatic(foo.class) from my test class, I'm given the excception:

java.lang.NoClassDefFoundError: org/Mockito/mock/MockName
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
at ...my test class method call...

I'm sure this is a setup problem, as I have been finding the terminology used for setting this up to be pretty confusing. I did grab the Mockito Zip from the PowerMock downloads. In Eclipse (3.5.2) I opened the project properties and added all of the Jars to the build path. I also tried adding the entire unzipped powermockito folder to my environment vars classpath, and then just the powermockito jar specifically when that didn't work out.

I have these annotationsat the class-level of my test class as well, per the powermock instructions:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ApplicationContextLoader.class)

Also these powermock-specific imports:

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

To those of you who have used PowerMockito before, even just a pointer in the right direction, or something to check would be really helpful. I'm struggling to see how my setup differs from that of posts I have seen using - from what I can tell - the same syntax.

like image 738
Ubunfu Avatar asked Sep 27 '13 16:09

Ubunfu


People also ask

How do you mock a static method in PowerMockito?

Use PowerMockito. mockStatic() for mocking class with static methods. Use PowerMockito. verifyStatic() for verifying mocked methods using Mockito.

How do you mock with PowerMockito?

To use PowerMock with Mockito, we need to apply the following two annotations in the test: @RunWith(PowerMockRunner. class): It is the same as we have used in our previous examples. The only difference is that in the previous example we have used MockitoUnitRunner.

Can static classes be mocked?

The powerful capabilities of the feature-rich JustMock framework allow you to mock static classes and calls to static members like methods and properties, set expectations and verify results. This feature is a part of the fastest, most flexible and complete mocking tool for crafting unit tests.


2 Answers

Probably, the versions of Powermock and Mockito are not compatible. Fix that and it won't be a n issue anymore.

Mockito                     PowerMock
1.10.8+                     1.6.2+
1.9.5-rc1 - 1.9.5           1.5.0 - 1.5.6
1.9.0-rc1 & 1.9.0           1.4.10 - 1.4.12
1.8.5                       1.3.9 to 1.4.9
1.8.4                       1.3.7 & 1.3.8 
1.8.3                       1.3.6
1.8.1 & 1.8.2               1.3.5
1.8                         1.3
1.7                         1.2.5

See: https://github.com/powermock/powermock/wiki/Mockito#supported-versions

like image 146
Devs Avatar answered Oct 09 '22 11:10

Devs


If you are using a static mock object, in your PrepareForTest annotation, add the class that is USING the static object in addition to the static class itself. If the class you are testing needs to use this static, add the current class to the annotation. You don't actually mock the class, but it needs to be in the annotation for the static to hook in. It sounds weird, but it works.

When adding multiple classes into the annotation you can have them inside {} and seperated by commas. For example if your static class is StaticA.class and the class using the static is CallerOfStatic.class you can use:

@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticA.class, CallerOfStatic.class})
like image 38
Walls Avatar answered Oct 09 '22 10:10

Walls