Question

In a Maven Project I am using PowerMock-easymock to run jUnit test cases. but while doing "mvn clean install" , I am getting below output..


T E S T S

Running TestSuite Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.621 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0


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

<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, PowerMock dependency and do "mvn clean install" , all test cases are running fine. But I have to use PowerMock. How to solve this issue?

Was it helpful?

Solution

I guess that some of your test cases are not running, did you try this

  1. Use mvn surefire plugin, and include test cases in that.

  2. ensure dependancy of powermock-module-junit4.

    check out these link: code.google.com/p/powermock/wiki/EasyMock_maven http://code.google.com/p/powermock/wiki/GettingStarted

OTHER TIPS

I had the same problem, and it took me a while to figure out. My setup was pulling in an older version of jboss.javassist, which oddly was preventing the PowerMockRunner from working at all.

It's worth noting that I also have a mixed JUnit / TestNG environment. I previously tried the solution of adding multiple surefire 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 running without needing to declare any surefire providers. In fact, the JUnit provider threw an error for me, because I'm using groups. Apparently the TestNG provider allows free-form text (e.g. "integration") while the JUnit provider expects a classpath (e.g. "com.example.UnitTests").

Here's 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 the relevant testing deps ...

    <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>

As per 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 Correct ArtifactId in dependency , you can see this link if you are using jUnit.

Then do "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>

Refer this link for detail

All credits go to Dipak

I had a situation recently where PowerMock tests were run, but not included in the surefire coverage report. We found that the problem had to do with instrumentation.

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

Here is the 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>

The <systemPropertyVariables> are probably not relevant to the fix.

Also, note that JaCoCo's documentation warns against using this type of configuration unless you find it necessary.

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

Warning: The preferred way for code coverage analysis with JaCoCo is on-the-fly instrumentation. Offline instrumentation has several drawbacks and should only be used if a specific scenario explicitly requires this mode. Please consult documentation about offline instrumentation before using this mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top