Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powermock and mockito compatibility and changes

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

like image 901
DanutClapa Avatar asked Jan 01 '23 11:01

DanutClapa


1 Answers

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>
like image 122
second Avatar answered Jan 13 '23 14:01

second