我在使用evosuite时与jacoco产生了冲突导致jacoco代码覆盖率为0,找到了原因所在处,是自动生成代码的配置在pom文件中不生效,问题出在哪里。下面是官方文档的错误产生内容及解决方案和我的部分代码。
现在我希望的是pom文件中的配置能够生效,自动生成的Test中separateClassLoader = false。
官方文档https://www.evosuite.org/documentation/measuring-code-coverage/



pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.coderdream</groupId>
<artifactId>bsTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ant-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<evosuiteVersion>1.0.6</evosuiteVersion>
<customFolder>src/test/java</customFolder>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.evosuite</groupId>
<artifactId>evosuite-standalone-runtime</artifactId>
<version>${evosuiteVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.evosuite.plugins</groupId>
<artifactId>evosuite-maven-plugin</artifactId>
<version>${evosuiteVersion}</version>
<configuration>
<extraArgs>-Duse_separate_classloader=false</extraArgs>
</configuration>
<executions><execution>
<goals> <goal> prepare </goal> </goals>
<phase> process-test-classes </phase>
</execution></executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<properties>
<property>
<name>listener</name>
<value>org.evosuite.runtime.InitializingListener</value>
</property>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${customFolder}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
ANT build文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:jacoco="antlib:org.jacoco.ant" name="bsTest" default="all" basedir=".">
<!--源代码src路径-->
<property name="src.dir" value="src/main/java"/>
<!--单元测试代码路径-->
<property name="test.dir" value="src/test/java"/>
<!--编译文件class路径-->
<property name="build.dir" value="build"/>
<!--lib包路径-->
<property name="lib.dir" value="lib"/>
<!--生成报告Junit4.xml路径-->
<property name="report.dir" value="report"/>
<property name="MAVEN_HOME" value="C:\Program Files\JetBrains\IntelliJ IDEA 2020.2\plugins\maven\lib\maven3"/>
<!--jacoco-->
<property name="result.dir" location="./target"/>
<property name="result.report.dir" location="${result.dir}/site/jacoco"/>
<property name="result.exec.file" location="${result.dir}/jacoco.exec"/>
<!--<property name="jacocoantPath" location="lib/jacocoant.jar"/>-->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="lib/jacocoant.jar"/>
</taskdef>
<!--设置classpath-->
<path id="compile.path">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<!--初始化,新建文件夹-->
<target name="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${report.dir}"/>
</target>
<!--清除-->
<target name="clean" description="clean">
<delete dir="${build.dir}"/>
<delete dir="${report.dir}"/>
<delete dir="${result.dir}"/>
<delete dir="${test.dir}"/>
</target>
<!--调用maven的evosuite命令生成Junit测试用例,每个参数必须占据一行-->
<target name="compile-evosuite">
<exec executable="mvn.cmd">
<arg value="-f"/>
<arg value="D:/mycode/bsTest/pom.xml"/>
<arg value="compile"/>
<!--<arg value="-Dcores=4"/>-->
<!--<arg value="-Duse_separate_classloader=false"/>-->
<arg value="-DtargetFolder=src/test/java"/>
<arg value="evosuite:generate"/>
<arg value="evosuite:export"/>
</exec>
</target>
<!--编译测试文件,初始化目录-->
<target name="compile" description="编译">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="compile.path" includeantruntime="on" debug="true"/>
<javac srcdir="${test.dir}" destdir="${build.dir}" classpathref="compile.path" includeantruntime="on" debug="true"/>
</target>
<!--执行测试案例-->
<target name="junit">
<jacoco:coverage destfile="${result.exec.file}">
<junit printsummary="true" fork="true">
<formatter type="xml" usefile="true"/>
<classpath>
<!--添加测试用例目录-->
<pathelement location="${build.dir}"/>
<!--添加依赖库-->
<path refid="compile.path"/>
</classpath>
<!--执行多个测试用例-->
<batchtest fork="on" todir="${report.dir}" haltonfailure="no">
<fileset dir="${build.dir}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<!-- 产生单元测试报表文档 -->
<junitreport todir="${report.dir}">
<fileset dir="${report.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${report.dir}"/>
</junitreport>
</target>
<!-- 生成覆盖率报告 -->
<target name="report">
<jacoco:report>
<executiondata>
<file file="${result.exec.file}" />
</executiondata>
<structure name="JaCoCo Ant Example">
<classfiles>
<!--覆盖率报告的类-->
<fileset dir="${build.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>
<target name="all" depends="clean,init,compile-evosuite,compile,junit,report"/>
</project>
自动生成的一个测试用例
/*
* This file was automatically generated by EvoSuite
* Wed Apr 12 08:33:13 GMT 2023
*/
import org.junit.Test;
import static org.junit.Assert.*;
import static org.evosuite.runtime.EvoAssertions.*;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.junit.runner.RunWith;
@RunWith(EvoRunner.class) @EvoRunnerParameters(mockJVMNonDeterminism = true, useVFS = true, useVNET = true, resetStaticState = true, separateClassLoader = true, useJEE = true)
public class ComplexCalculation_ESTest extends ComplexCalculation_ESTest_scaffolding {
@Test(timeout = 4000)
public void test0() throws Throwable {
ComplexCalculation complexCalculation0 = new ComplexCalculation();
int int0 = complexCalculation0.Multiply(0, 0);
assertEquals(0, int0);
}
@Test(timeout = 4000)
public void test1() throws Throwable {
ComplexCalculation complexCalculation0 = new ComplexCalculation();
int int0 = complexCalculation0.Multiply((-3998), (-1933));
assertEquals(7728134, int0);
}
@Test(timeout = 4000)
public void test2() throws Throwable {
ComplexCalculation complexCalculation0 = new ComplexCalculation();
int int0 = complexCalculation0.Division(1558, (-18696));
assertEquals(0, int0);
}
@Test(timeout = 4000)
public void test3() throws Throwable {
ComplexCalculation complexCalculation0 = new ComplexCalculation();
int int0 = complexCalculation0.Division((-1610), 130);
assertEquals((-12), int0);
}
@Test(timeout = 4000)
public void test4() throws Throwable {
ComplexCalculation complexCalculation0 = new ComplexCalculation();
// Undeclared exception!
try {
complexCalculation0.Division(1, 0);
fail("Expecting exception: ArithmeticException");
} catch(ArithmeticException e) {
//
// / by zero
//
verifyException("ComplexCalculation", e);
}
}
@Test(timeout = 4000)
public void test5() throws Throwable {
ComplexCalculation complexCalculation0 = new ComplexCalculation();
int int0 = complexCalculation0.Multiply((-1610), 864);
assertEquals((-1391040), int0);
}
@Test(timeout = 4000)
public void test6() throws Throwable {
ComplexCalculation complexCalculation0 = new ComplexCalculation();
int int0 = complexCalculation0.Division((-1610), (-1610));
assertEquals(1, int0);
}
}