Ignore methods in class. cobertura maven plugin

百般思念 提交于 2019-12-04 15:56:41
boumbh

If it's only about getters and setters, you could set the ignoreTrival switch:

Cobertura Changelog - New --ignoreTrivial switch that tells Cobertura to ignore the following in the coverage report: Getter methods that simply read a class field; Setter methods that set a class field; Constructors that only set class fields and call a super class constructor.

Source: Is there still no solution for ignoring setter/getter (other trivial methods) with the cobertura-maven-plugin?

If you wish to ignore methods more specifically, you could also use the ignoreMethodAnnotation switch:

Cobertura Changelog - New --ignoreMethodAnnotation switch used to specify an annotation that, when present on a method, will cause Cobertura to ignore the method in the coverage report.

Documentation: Documentation of ignoreMethodAnnotation (they made a small mistake: they define the CoberturaIgnore annotation but then they use CoverageIgnore)

Example in the pom.xml:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>2.7</version>
  <configuration>
    <instrumentation>
      <ignoreTrivial>true</ignoreTrivial>
      <ignoreMethodAnnotation>foo.bar.CoberturaIgnore</ignoreMethodAnnotation>
    </instrumentation>
  </configuration>
</plugin>
Kane

I don't know about ignoring methods, but you could take a tool that autogenerates the unit tests for your getters and setters so those methods are covered. I don't know that this fixes your exact problem, because now instead of lower than expected coverage you'll have higher than expected coverage, but it seems better than nothing.

There was a SO question about exactly this here: Is there a Java unit-test framework that auto-tests getters and setters?

We are using Cobertura for Spring-Boot Restful API Unit Test Code Coverage metrics.

We had similar situation In order to ignore some of the classes from unit testing code coverage metrics reporting for the classes with hibernate native queries which cannot be unit tested with JPATest classes(as in memory DB).. for any reason if you need cobertura to ignore some of your classes from these metrics then do the following...

pom.xml

<build>
        <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                    <instrumentation>
                        <ignoreTrivial>true</ignoreTrivial>
                        <ignoreMethodAnnotation>com.thermofisher.micro.common.annotation.CoberturaIgnore</ignoreMethodAnnotation>           
                    </instrumentation>
                </configuration>
                <executions>
                  <execution>
                    <goals>
                      <goal>clean</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
      <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                    <instrumentation>
                        <ignoreTrivial>true</ignoreTrivial>
                        <ignoreMethodAnnotation>com.thermofisher.micro.common.annotation.CoberturaIgnore</ignoreMethodAnnotation>           
                    </instrumentation>
                </configuration>
            </plugin>
       </plugins>
    </reporting>

Annotation

package com.any.package.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PACKAGE})
public @interface CoberturaIgnore {}

Put the annotation to use:

package com.any.package.repository;

import com.any.package.annotation.CoberturaIgnore;

@CoberturaIgnore
@Repository
public class SequenceIdRespositoryImpl implements SequenceIdRespository {

    @PersistenceContext
    private EntityManager entityManager;


    @CoberturaIgnore
    @Override
    public long getUserContactKey() {
        Query query = entityManager.createNativeQuery("select user_key_seq.nextval from dual");
        return getKey(query);
    }
}   
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!