Jacoco 不检测接口
Jacoco doesn't detect interfaces
我已配置 Jacoco,以便它在单元测试时生成覆盖率报告 运行。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>unit-test-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefireArgLine</propertyName>
<destFile>${jacoco.report.directory}/jacoco-ut.exec</destFile>
</configuration>
</execution>
<execution>
<id>unit-test-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${jacoco.report.directory}/jacoco-ut.exec</dataFile>
<outputDirectory>${jacoco.report.directory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
但出于某种原因,它跳过了项目的 dao 包,其中包含 Spring Data Jpa
个存储库接口。
例如下面的界面:
import com.shaunyl.website.dao;
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {
@Query(value = "SELECT p FROM Product p",
countQuery = "SELECT COUNT(p) FROM Product p")
Page<Product> findAll(Pageable pageable);
}
有以下测试:
@RunWith(SpringRunner.class)
@DataJpaTest
public class ProductRepositoryTests {
private static final int INVENTORY_SIZE = 5;
@Autowired
ProductRepository productRepository;
private Category[] categories;
private List<Product> inventory;
@Before
public void setUp() {
inventory = productRepository.saveAll(products(INVENTORY_SIZE));
categories = inventory.stream().map(Product::getCategory).toArray(Category[]::new);
}
@Test
public void shouldRetrieveOnePageOfProducts() {
// given
int PAGE = 0;
int SIZE = 20;
Pageable pageable = newUnsortedPage(PAGE, SIZE);
// when
Page<Product> products = productRepository.findAll(pageable);
// then
assertThat(products.getNumber()).isEqualTo(PAGE);
assertThat(products.getNumberOfElements()).isEqualTo(INVENTORY_SIZE);
assertThat(products)
.as("categories are eagerly fetched")
.extracting(Product::getCategory)
.containsExactlyInAnyOrder(categories);
}
}
但是在Jacoco报告中,dao包被跳过了。
我相信这是因为目标 class 是一个接口,但我不确定。
您知道可能是什么问题以及如何解决吗?
JaCoCo 测量可执行Java代码。在你的界面中
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {
@Query(value = "SELECT p FROM Product p",
countQuery = "SELECT COUNT(p) FROM Product p")
Page<Product> findAll(Pageable pageable);
}
没有可执行的Java代码,只有方法声明和使用常量的注释。
这在JaCoCo FAQ中也有解释:
Why are abstract methods not shown in coverage reports?
Abstract methods do not contain code, therefore code coverage cannot be evaluated. Indeed code coverage is recorded for subclasses implementing these methods. The same applies to non-default methods in interfaces.
我已配置 Jacoco,以便它在单元测试时生成覆盖率报告 运行。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>unit-test-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefireArgLine</propertyName>
<destFile>${jacoco.report.directory}/jacoco-ut.exec</destFile>
</configuration>
</execution>
<execution>
<id>unit-test-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${jacoco.report.directory}/jacoco-ut.exec</dataFile>
<outputDirectory>${jacoco.report.directory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
但出于某种原因,它跳过了项目的 dao 包,其中包含 Spring Data Jpa
个存储库接口。
例如下面的界面:
import com.shaunyl.website.dao;
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {
@Query(value = "SELECT p FROM Product p",
countQuery = "SELECT COUNT(p) FROM Product p")
Page<Product> findAll(Pageable pageable);
}
有以下测试:
@RunWith(SpringRunner.class)
@DataJpaTest
public class ProductRepositoryTests {
private static final int INVENTORY_SIZE = 5;
@Autowired
ProductRepository productRepository;
private Category[] categories;
private List<Product> inventory;
@Before
public void setUp() {
inventory = productRepository.saveAll(products(INVENTORY_SIZE));
categories = inventory.stream().map(Product::getCategory).toArray(Category[]::new);
}
@Test
public void shouldRetrieveOnePageOfProducts() {
// given
int PAGE = 0;
int SIZE = 20;
Pageable pageable = newUnsortedPage(PAGE, SIZE);
// when
Page<Product> products = productRepository.findAll(pageable);
// then
assertThat(products.getNumber()).isEqualTo(PAGE);
assertThat(products.getNumberOfElements()).isEqualTo(INVENTORY_SIZE);
assertThat(products)
.as("categories are eagerly fetched")
.extracting(Product::getCategory)
.containsExactlyInAnyOrder(categories);
}
}
但是在Jacoco报告中,dao包被跳过了。 我相信这是因为目标 class 是一个接口,但我不确定。
您知道可能是什么问题以及如何解决吗?
JaCoCo 测量可执行Java代码。在你的界面中
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {
@Query(value = "SELECT p FROM Product p",
countQuery = "SELECT COUNT(p) FROM Product p")
Page<Product> findAll(Pageable pageable);
}
没有可执行的Java代码,只有方法声明和使用常量的注释。
这在JaCoCo FAQ中也有解释:
Why are abstract methods not shown in coverage reports?
Abstract methods do not contain code, therefore code coverage cannot be evaluated. Indeed code coverage is recorded for subclasses implementing these methods. The same applies to non-default methods in interfaces.