5

I am trying to move to power mock 2.0.2 and mockito 2.0. Previously I used powermock to mock some local variables: I used on test class

 @RunWith(PowerMockRunner.class) @PrepareForTest({MyClass.class}) whenNew(MyClass.class).withAnyArguments().thenReturn(myClassMock); 

All is fine with Power mock 1.6.. When I tried to move to Powermock 2.x I can not find anymore the whenNew() method in PowerMock. How this changed in the new Powermock? Dependencies:

<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>2.0.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.15.0</version> <scope>test</scope> </dependency> 

And by the way which version of mockito is compatible wit Powermock? - i see there are some problems of support of Powermock with mockito

4
  • The whenNew method is still in the PowerMockito class. You probably have some issues with your dependencies. What is the mockito version you are trying to use? Afaik for 2.0 only beta versions exists and not all of them are supported by powermockito (check the wiki). Commented Nov 28, 2019 at 11:19
  • I edited the post with dependencies. I am tring ro move to 2.x. I also I dont know if it is a good idea. Commented Nov 28, 2019 at 12:00
  • powermock 2.0.4 requires mockito 2.8.9+, give that combination a try Commented Nov 28, 2019 at 12:40
  • Strange i am trying now: powermock-api-mockito and powermock-module-junit4 version 2.0.4 and mockito-core 3.1.0. Still no whenNew method. Can you point me to a good example with the corect setup of PoerMock and mockito? Commented Nov 28, 2019 at 13:38

1 Answer 1

10

I missed the part where you defined the old api component, try powermock-api-mockito2.
This is what I use in one of my projects (which defaults to mockito-core 2.28.2)

<dependencies> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.4</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> 
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, yes moed from powermock 1 to 2 and i should change to powermock-api-mockito2. That was the problem, Thank you!
I had to also add powermock-core for testing static method's mock

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.