Как видно из названия, я запускаю Java 11 в проекте maven, и я начал использовать JaCoCo для измерения покрытия тестами.
-<package name="">
-<class name="Calculator" sourcefilename="Calculator.java">
-<method name="<init>" line="4" desc="()V">
<counter covered="3" missed="0" type="INSTRUCTION"/>
<counter covered="1" missed="0" type="LINE"/>
<counter covered="1" missed="0" type="COMPLEXITY"/>
<counter covered="1" missed="0" type="METHOD"/>
</method>
-<method name="sum" line="6" desc="(II)I">
<counter covered="4" missed="0" type="INSTRUCTION"/>
<counter covered="1" missed="0" type="LINE"/>
<counter covered="1" missed="0" type="COMPLEXITY"/>
<counter covered="1" missed="0" type="METHOD"/>
</method>
-<method name="mulitpliy" line="9" desc="(II)I">
<counter covered="0" missed="4" type="INSTRUCTION"/>
<counter covered="0" missed="1" type="LINE"/>
<counter covered="0" missed="1" type="COMPLEXITY"/>
<counter covered="0" missed="1" type="METHOD"/>
</method>
<counter covered="7" missed="4" type="INSTRUCTION"/>
<counter covered="2" missed="1" type="LINE"/>
<counter covered="2" missed="1" type="COMPLEXITY"/>
<counter covered="2" missed="1" type="METHOD"/>
<counter covered="1" missed="0" type="CLASS"/>
</class>
Выше приведен пример вывода результатов тестирования двух методов умножения и суммирования в классе калькулятор. То, что я хотел бы знать, как я могу ie это к определенному c тесту?
Вот как я узнаю, что это:
@Test
public void testSum(){
assertEquals(10,calc.sum(5,5));
}
Протестирован следующий код:
public class Calculator {
public int sum(int a, int b){
return a+b;
}
}
Файл POM выглядит следующим образом:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>JacocoTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<!-- https://mvnrepository.com/artifact/org.jacoco/jacoco-maven-plugin -->
<build>
<plugins>
<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>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/my-reports</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
Если это невозможно, я приветствую альтернативы JaCoCo, которые могут экспортировать результат в XML или JSON.