Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cobertura : how to cover spring-data @Repository interfaces

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.

like image 899
Saad Benbouzid Avatar asked Nov 09 '15 11:11

Saad Benbouzid


People also ask

How do you exclude certain interfaces from instantiation as repository?

To exclude an interface extending Repository from being instantiated as a repository instance it can either be annotate it with @NoRepositoryBean or moved out side of the configured base-package .

Is @repository mandatory in Spring boot?

It is indeed not necessary to put the @Repository annotation on interfaces that extend JpaRepository ; Spring recognizes the repositories by the fact that they extend one of the predefined Repository interfaces. From the javadoc: Annotation to enable JPA repositories.

What does @repository do in Spring?

@Repository is a Spring annotation that indicates that the decorated class is a repository. A repository is a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects.

What are the important predefined repository interfaces and classes in Spring data JPA?

Some important Pre Defined interfaces and classes of Spring Data JPA. The Repository is a core interface, which is a marker interface. The CrudRepository interface extends Repository interface, contains the following method. save(S entity) – Used to save a single entity at a time.


1 Answers

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.

like image 197
Duong Nguyen Avatar answered Dec 23 '22 09:12

Duong Nguyen