我正在尝试找出应用程序的代码覆盖率。我已经为他们编写了黑匣子功能测试。 我正在关注this教程。
我的步骤:
我从-https://github.com/llatinov/sample-dropwizard-rest-stub下载了源代码 并在pom中添加了以下更改:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<executions>
<execution>
<id>jacoco-instrument</id>
<phase>test</phase>
<goals>
<goal>instrument</goal>
</goals>
<configuration>
<skip>${jacoco.skip.instrument}</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
使用mvn clean程序包运行
使用Java反编译器工具来验证是否检测了类。我可以看到正在检测的代码:
import org.jacoco.agent.rt.internal_43f5073.Offline;
@Path("/books")
public class BookService {
public BookService() {
arrayOfBoolean[0] = true;
}
@GET
@Timed
@Produces({"application/json"})
public List<Book> getBooks() {
boolean[] arrayOfBoolean = $jacocoInit();
arrayOfBoolean[1] = true;
return BookDB.getAll();
}
}
我使用以下命令启动了该应用程序:
java -Djacoco-agent.output=tcpserver -Djacoco-agent.destfile=/home/sb/Desktop/jacoco.exec -Djacoco-agent.includes=* -Djacoco-agent.dumponexit=false -Djacoco-agent.address=* -Djacoco-agent.port=6300 -cp /home/sb/Downloads/jacoco/lib/jacocoagent.jar:target/sample-dropwizard-rest-stub-1.0-SNAPSHOT.jar com.automationrhapsody.reststub.RestStubApp server config.yml
我创建了一个Maven测试项目并执行了该应用程序的测试。但是仍然没有创建jacoco.exec文件。我用于测试项目的pom.xml是:
<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>TestJACOCOApp</groupId>
<artifactId>TestJACOCOApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
请注意,即使关闭了JVM,也不会在指定位置创建jacoco.exec文件。我的喷气机jacocoagent jar文件的权限为755。