All jUnit test cases don't work for Maven project using PowerMock with easymock, Surefire

In the Maven project, I use PowerMock-easymock to run jUnit test cases. but by doing "mvn clean install" I get below output.


TESTS

Running TestSuite Spaces: 2, Errors: 0, Errors: 0, Missed: 0, Elapsed Time: 0.621 seconds

Results:

Test runs: 2, Errors: 0, Errors: 0, Missed: 0


But I have many other test cases. Here is the pom.xml part

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-easymock-release-full</artifactId> <version>1.4.12</version> <type>pom</type> </dependency> 

If I remove the PowerMock dependency and make "mvn clean install", all test cases work fine. But I have to use PowerMock. How to solve this problem?

+4
source share
4 answers

I assume some of your test cases do not work, you tried this

+6
source

I had the same problem and it took me a while to figure it out. My installation occupied an older version of jboss.javassist, which, oddly enough, prevented PowerMockRunner from working at all.

It is worth noting that I also have a mixed JUnit / TestNG environment. I previously tried the decision to add several shipyard providers, and that didn't work either (using surefire 2.14.1). After upgrading to surefire 2.17, both my JUnit and TestNG tests started working without the need to declare any warranty providers. In fact, the JUnit provider threw me an error because I use groups. The TestNG provider seems to allow free-form text (for example, "integration"), while the JUnit provider expects a class path (for example, "com.example.UnitTests").

Here is my plugin section ...

  <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.17</version> <configuration> <groups>spring, unit, integration</groups> <systemPropertyVariables> <java.awt.headless>true</java.awt.headless> <org.apache.activemq.default.directory.prefix>target/test/</org.apache.activemq.default.directory.prefix> <log4j.configuration>file:${project.basedir}/src/test/resources/log4j.properties</log4j.configuration> </systemPropertyVariables> <argLine>${surefire.args}</argLine> </configuration> </plugin> 

... and related analytic prints ...

  <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> <!-- PowerMock versions are compatible with specific Mockito versions. https://code.google.com/p/powermock/wiki/MockitoUsage13 --> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.5.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.5.4</version> <scope>test</scope> </dependency> <!-- without this PowerMock tests don't run in maven --> <dependency> <groupId>jboss</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> <scope>test</scope> </dependency> 
+2
source

According to dipak,

Solution 1: Add below code in pom.xml

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.13</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.13</version> </dependency> </dependencies> </plugin> 

For the correct ArtifactId depending, you can see the link if you are using jUnit.

Then run "mvn clean install"

Solution 2: Add below code in pom.xml

 <properties> <powermock.version>1.5</powermock.version> </properties> <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-easymock</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> 

See the link for more details.

All loans go to Dipak

0
source

I recently had a situation where PowerMock tests were tested but not included in the coverage report. We found that the problem is with the tools.

I note that most of our tests work with TestNG. In general, we only use JUnit when we need to use PowerMock.

Here is a POM snippet:

 <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <systemPropertyVariables> <org.apache.activemq.default.directory.prefix>target/test/</org.apache.activemq.default.directory.prefix> <log4j.configuration>file:${project.basedir}/src/test/resources/log4j.properties</log4j.configuration> <jacoco-agent.destfile>${project.basedir}/target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> <argLine>-Xmx512m -XX:MaxPermSize=256m -Djava.awt.headless=true</argLine> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.5.201505241946</version> <executions> <execution> <id>instrument</id> <phase>process-classes</phase> <goals> <goal>instrument</goal> </goals> </execution> <execution> <id>restore</id> <phase>test</phase> <goals> <goal>restore-instrumented-classes</goal> <goal>report</goal> </goals> </execution> </executions> </plugin> 

<systemPropertyVariables> is probably not a fix.

Also note that the JaCoCo documentation warns you about using this type of configuration if you do not consider it necessary.

http://www.eclemma.org/jacoco/trunk/doc/instrument-mojo.html

A warning. The preferred way to analyze coverage code using JaCoCo is on the fly. There are several drawbacks in autonomous instrument engineering and should be used only if a specific scenario clearly requires this mode. Please refer to the offline documentation before using this mode.

0
source

All Articles