我正在使用带有maven的maven-surefire-plugin版本2.17。
我正在使用jacoco-maven-plugin来分析我的junit测试:
我在pom.xml中设置的jacoco插件如下所示:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>agent-for-ut</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
我在src / main / java / something / excel下面有一个类,如下所示:
package something.excel;
public class VDHTCStyle
{
public int doSomething() {
int i=0;
return i+7;
}
}
我在src / test / java / something / excel下的测试类看起来像这样:
package something.excel;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class VDHTCStyleStyleTest
{
private VDHTCStyleStyle vDHTCStyleStyle;
@Before
public void setUp()
{
vDHTCStyleStyle = new VDHTCStyleStyle();
}
@Test
public void shouldDoSomething() {
int something = vDHTCStyleStyle.doSomething();
assertEquals(something, 7);
}
}
当我用
运行时mvn clean install
我在日志中看到了这一点:
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ xxxxx ---
[INFO] Surefire report directory: /var/lib/jenkins/workspace/xxxx/modules/xx/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running something.excel.VDHTCStyleTest
|classnames | 2.2.3 | A simple utility for conditionally joining classNames together|
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec - in something.excel.VDHTCStyleTest
现在,如果我查看生成的jacoco-ut报告文件,我会看到类似这样的内容:
GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED,INSTRUCTION_COVERED,BRANCH_MISSED,BRANCH_COVERED,LINE_MISSED,LINE_COVERED,COMPLEXITY_MISSED,COMPLEXITY_COVERED,METHOD_MISSED,METHOD_COVERED
Module: xxx,something.excel,VDHTCStyleStyle,9,0,0,0,3,0,2,0,2,0
据我了解,它认为没有涵盖任何行,说明,分支或方法。
为什么呢?帮助:)
答案 0 :(得分:1)
问题似乎是maven-surefire-plugin在多模块项目中指定两次时表现不可预测
我的父pom在构建部分中有它,而子模块pom也在构建部分中有它。因此,它似乎正在运行测试,但子模块中的覆盖率始终为0。
修复是从子模块pom中删除maven-surefire-plugin,并且只在父pom的build部分中有它。
问题得到了解决。