在Maven中使用JMockit和JUnit运行EMMA

时间:2014-06-20 05:52:33

标签: maven-2 junit4 jmockit

在maven中使用JMockit + JUnit运行EMMA代码覆盖率工具时遇到问题。

我有一个项目,我使用JMockit作为其中的模拟框架。

运行 mvn test 后,它运行成功没有任何问题。这意味着JMockit正在以适当的方式初始化JUnit。

以下是我在POM中定义JMockit和JUnit的依赖关系的方式(按照确切的顺序)。

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

但是当我用EMMA检查项目的代码覆盖率时,它说

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)

但我认为我已经正确配置了EMMA插件,如下所示,

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

有人能抓到那里的错误吗?

1 个答案:

答案 0 :(得分:1)

我能够弄清楚那里出了什么问题。看来我们需要特别说JUnit在使用EMMA时使用JMockit。

我们可以使用maven-surefire-plugin来实现。

我们需要将以下配置添加到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>

注意:确保在上面的配置中更改JMockit Jar的位置。

此外,我们不需要在EMMA插件配置中具有依赖关系。只需将它们放在POM的依赖关系部分(按照确切的顺序)就足够了。

参考文献: JMockit - initialization problem

相关问题