使用@Autowired 对接口实现的 JaCoCo 代码覆盖率
JaCoCo code coverage on interface implementations using @Autowired
我已经为我的测试创建了一个包含默认方法的接口。目前,它看起来像这样:
public interface CRUDTest<
Controller extends ControllerCRUD<Model, DTO, Creation, Update, Service>,
Service extends ServiceCRUD<Model, Creation, Update, ? extends GenericRepository<Model>>,
Creation extends CreationDTO<Model>,
Update extends UpdateDTO<Model>,
DTO extends ModelDTO,
Model extends GenericModel> {
Controller getController();
Service getService();
ImageService getImageService();
Creation generateCreationDTO();
default void doStuff() {
service().createFromDTO(generateCreationDTO());
// ...
}
}
然后,每个测试按以下方式实现此接口:
public class Implementation implements CRUDTest<ExampleController, ExampleService, ExampleCreationDTO, ExampleUpdateDTO, ExampleDTO, ExampleModel> {
@Autowired @Getter private SongService service;
@Autowired @Getter private SongController controller;
@Autowired @Getter private ImageService imageService;
@Test
public void doStuff() {
CRUDTest.super.doStuff();
}
}
据我所知,我服务中的 "createFromDTO" 方法现在应该被报告为 JaCoCo 涵盖的,并且在 运行 测试时显然会调用它。但是,JaCoCo 报告该方法未被发现,所以我想知道我可能遗漏了什么。
我找到问题了!我正在从事的项目遵循多模块结构,一些集成测试包括来自不同项目的方法。因此 JaCoCo 没有涵盖这些方法,但一些谷歌搜索让我相信有几种方法可以解决这个问题。
我已经为我的测试创建了一个包含默认方法的接口。目前,它看起来像这样:
public interface CRUDTest<
Controller extends ControllerCRUD<Model, DTO, Creation, Update, Service>,
Service extends ServiceCRUD<Model, Creation, Update, ? extends GenericRepository<Model>>,
Creation extends CreationDTO<Model>,
Update extends UpdateDTO<Model>,
DTO extends ModelDTO,
Model extends GenericModel> {
Controller getController();
Service getService();
ImageService getImageService();
Creation generateCreationDTO();
default void doStuff() {
service().createFromDTO(generateCreationDTO());
// ...
}
}
然后,每个测试按以下方式实现此接口:
public class Implementation implements CRUDTest<ExampleController, ExampleService, ExampleCreationDTO, ExampleUpdateDTO, ExampleDTO, ExampleModel> {
@Autowired @Getter private SongService service;
@Autowired @Getter private SongController controller;
@Autowired @Getter private ImageService imageService;
@Test
public void doStuff() {
CRUDTest.super.doStuff();
}
}
据我所知,我服务中的 "createFromDTO" 方法现在应该被报告为 JaCoCo 涵盖的,并且在 运行 测试时显然会调用它。但是,JaCoCo 报告该方法未被发现,所以我想知道我可能遗漏了什么。
我找到问题了!我正在从事的项目遵循多模块结构,一些集成测试包括来自不同项目的方法。因此 JaCoCo 没有涵盖这些方法,但一些谷歌搜索让我相信有几种方法可以解决这个问题。