0:Supported versions
PowerMock version 1.7.0 and upper has experimental support of Mockito 2.
A lot of issues are not resolved still. PowerMock uses internal Mockito API, but at least it possible to use both mocking framework together.
PowerMock team working on full Mockito 2 with Mockito team. (#726, #mockito/1110)
1:PowerMock+Mockito+Junit4.4 -Abovehtml
<properties> <powermock.version>1.7.1</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-mockito2</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> </dependencies>
2:PowerMock+Mockito+TestNGjava
<properties> <powermock.version>1.7.1</powermock.version> </properties> <dependencies> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-testng</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> </dependencies>
3:Demo Projectapache
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx.unittest</groupId> <artifactId>xxxTest</artifactId> <version>0.1.0</version> <packaging>jar</packaging> <name>xxxTest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <powermock.version>1.7.1</powermock.version> <jacoco.version>0.7.9</jacoco.version> <surefireArgLine> -javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar=destfile=${project.build.directory}/jacoco.exec </surefireArgLine> </properties> <parent> <groupId>com.xxx.project</groupId> <artifactId>ParentPom</artifactId> <version>4.0.0</version> </parent> <dependencies> <!--storm public package && demo <dependency> <groupId>com.xxx.service</groupId> <artifactId>xxxStormTools</artifactId> <version>0.1.3</version> </dependency> --> <!-- NoClassDefFoundError: com/google/inject/Injector --> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.1.0</version> </dependency> <!--powermock && mockito &&testng--> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-testng</artifactId> <version>${powermock.version}</version> <!--<scope>test</scope>--> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>${powermock.version}</version> <!--<scope>test</scope>--> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-testng-agent</artifactId> <version>1.7.1</version> <!--<scope>test</scope>--> </dependency> <dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> </dependency> <!--jacoco--> <dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.agent</artifactId> <version>0.7.9</version> </dependency> <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> </dependency> <!--jcommander --> <dependency> <groupId>com.beust</groupId> <artifactId>jcommander</artifactId> <version>1.72</version> </dependency> </dependencies> <build> <testSourceDirectory>src/test/java</testSourceDirectory> <resources> <resource> <directory>src/test/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!--jacoco--> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>pre-unit-test</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <destFile>${project.build.directory}/jacoco.exec</destFile> <!--Sets the name of the property containing the settings for JaCoCo runtime agent.--> <propertyName>surefireArgLine</propertyName> </configuration> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>${project.build.directory}/jacoco.exec</dataFile> <!-- Sets the output directory for the code coverage report. --> <!--<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>--> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <!-- 通過測試 maven-compiler-plugin 插件版本請使用3.3,不然在jenkins上沒法執行測試 --> <version>3.7.0</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <!--必須添加,不然不能生成jacoco覆蓋率的報告--> <testFailureIgnore>true</testFailureIgnore> <!-- 解決用maven執行test時 日誌亂碼的問題,詳見:http://www.cnblogs.com/discuss/archive/2010/10/27/1862225.html --> <argLine>-Dfile.encoding=UTF-8</argLine> <!--java8則須要添加-noverify配置--> <argLine>-noverify ${surefireArgLine} -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar </argLine> <systemPropertyVariables> <jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> <properties> <!--<property> <name>listener</name> <value>com.xxx.unittest.plugins.htmlReporter.TestReportListener</value> </property>--> <!-- 這個設置讓2個suite xml並行執行 --> <property> <name>suitethreadpoolsize</name> <value>1</value> </property> <property> <name>usedefaultlisteners</name> <value>false</value> </property> </properties> <suiteXmlFiles> <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> </suiteXmlFiles> <!--定義maven運行測試生成的報表路徑 --> <reportsDirectory>target/test-output</reportsDirectory> </configuration> </plugin> <!-- <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> </plugin> --> </plugins> </build> </project>