What jars do I need to add to my pom.xml
to get PowerMock working with Mockito? I have the following dependencies:
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-support</artifactId> <version>1.4.11</version> <scope>test</scope> </dependency>
but when I add the @PrepareForTest
annotation at class level, Eclipse cannot find it, but it can find PowerMockito
. What jar am I missing?
While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.
Power mock is used to mock static methods, private methods. Power mock is not compatible with JUnit5 So we will discuss it will JUnit4. Using power mock we can easily mock static methods. We can return a mocked value if some static method is called or can verify that the static method is called or not.
To include powermock in our application, add the powermock-api-mockito2 and powermock-module-junit4 dependencies. Note that there is no official extension for JUnit 5. If you plan to use its reflection module, for example invoking the private methods, then we need to import powermock-reflect module as well.
PowerMock is an open-source Java framework used for creating a mock object in unit testing. It extends other mocking frameworks such as EasyMock and Mockito to enhance the capabilities.
According to the Mockito_Maven page on the PowerMock wiki, use this:
<properties> <powermock.version>1.6.6</powermock.version> </properties> <dependencies> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> </dependencies>
powermock-api-support
seems to be "utility classes only", where you still need the core libraries provided in powermock-module-junit4
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With