Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Maven artifacts should I use to import PowerMock?

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?

like image 205
user86834 Avatar asked Sep 05 '13 20:09

user86834


People also ask

What is difference between Mockito and PowerMock?

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.

Is PowerMock compatible with JUnit 4?

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.

Does JUnit 5 support PowerMock?

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.

Is PowerMock a mocking framework?

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.


1 Answers

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.

like image 148
Jeff Bowman Avatar answered Oct 11 '22 19:10

Jeff Bowman