gbids
gbids

Reputation: 499

Running EMMA with JMockit and JUnit in Maven

I have a problem when running EMMA code coverage tool with JMockit + JUnit in maven.

I have a project and I am using JMockit as a mocking framework in it.

Once I run mvn test it is running successfully without any problem. That’s means JMockit is initializing with JUnit in a proper way.

Following is the way how I define my dependency for JMockit and JUnit within my POM (in the exact order).

<dependency>
    <groupId>com.googlecode.jmockit</groupId>
     <artifactId>jmockit</artifactId>
     <version>1.7</version>
 </dependency>

 <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.11</version>
     <scope>test</scope>
 </dependency>

But When I check the project for code coverage with EMMA, it says

java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation)

but I think I have configured EMMA plugin correctly and it is given below,

<build>
<defaultGoal>install</defaultGoal>
<plugins>

<!—Some other plugins here -->

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>emma-maven-plugin</artifactId>
        <version>1.0-alpha-3</version>
        <inherited>true</inherited>
        <configuration>
            <check>
                <classRate>100</classRate>
                <methodRate>100</methodRate>
                <blockRate>70</blockRate>
                <haltOnFailure>false</haltOnFailure>
            </check>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>com.googlecode.jmockit</groupId>
                <artifactId>jmockit</artifactId>
                <version>1.7</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>runtime</scope>
                <version>${junit.version}</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

Can anyone catch what is wrong there ?

Upvotes: 0

Views: 1228

Answers (1)

gbids
gbids

Reputation: 499

I was able to figured out what went wrong there. It seems that we need to specifically say JUnit to use JMockit when work with EMMA.

We can do it by using maven-surefire-plugin.

We need to add following configuration to the POM.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
    <argLine>-javaagent:${settings.localRepository}/com/googlecode/jmockit/jmockit/1.7/jmockit-1.7.jar</argLine>
    <useSystemClassLoader>true</useSystemClassLoader>
</configuration>
</plugin>

Note: Make sure to change the location of JMockit Jar in above configuration.

Further, we do not need to have dependencies within EMMA plugin configuration. Just having them in a dependencies section in POM (in exact order) will be enough.

References: JMockit - initialization problem

Upvotes: 1

Related Questions