Cobertura : how to cover spring-data @Repository interfaces

半腔热情 提交于 2019-12-09 16:12:03

问题


Regarding following information :

https://stackoverflow.com/a/14601831/704246

Cobertura does not instrument interfaces

I'd like to know how to add spring-data interfaces to coverage results, since @Repository implementation classes are only declared and instantiated by Spring at runtime.

Consider following interface :

// src/main/java/my/package/MyObjectRepository.java

@Repository
public interface MyObjectRepository  {
    MyObject findMyObjectByCodeAndName(String code, String name);
}

and following test :

// src/test/java/my/package/MyObjectRepositoryTest.java

// @RunWith(SpringJUnit4ClassRunner.class) + Spring configuration
public class MyObjectRepositoryTest {

    @Autowired
    private MyObjectRepository myObjectRepository;

    @Test
    public void myTest() {
        myObjectRepository.findMyObjectByCodeAndName("foo","bar");  
    }
}

I don't mind switching to Jacoco, but I've read that it doesn't instrument interfaces either.

How can following cases be covered ? The same issue/question occurs regarding Mybatis Mapper, which are declared as interfaces but no concrete Java class declaration implementing them is written by the developer but by the framework at runtime.

I've opened a ticket but I'm still waiting for an answer.


回答1:


If I understand correctly, an interface cannot be covered. Interfaces are just to define the contract, and contains no "runtime" commands. And code coverage tools only measure the lines that is reachable from the running tests. Say it other way, only field declaration, constructor or method body can be covered.

An exception may be Java8 interfaces that contains some default methods.



来源:https://stackoverflow.com/questions/33608075/cobertura-how-to-cover-spring-data-repository-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!