Ignore methods in class. cobertura maven plugin
Asked Answered
W

3

4

I have maven 3, cobertura maven plugin 2.51 and some classe. I need to know test coverage of my class. But I don't want to test setters/getters. So I wand just to ignore them.

 <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                    <check>
                        <haltOnFailure>false</haltOnFailure>
                        <lineRate>55</lineRate>
                        <branchRate>60</branchRate>
                        <packageLineRate>60</packageLineRate>
                        <packageBranchRate>60</packageBranchRate>
                        <totalLineRate>60</totalLineRate>
                        <totalBranchRate>60</totalBranchRate>
                    </check>
                    <instrumentation>
                        <excludes>
                            <exclude>com/FileCopier*.*</exclude>
                            <exclude>com/FileCopierWithCamel*.*</exclude>
                            <exclude>com/Main*.*</exclude>
                        </excludes>
                    </instrumentation>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>clean</goal>
                            <goal>cobertura</goal>
                            <goal>check</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>

Then I add following ignore block

 <ignores>
                            <!-- Ignore all setter and getter methods in your classes -->
                            <ignore>com.*.set*</ignore>
                            <ignore>com.*.get*</ignore>
                            <ignore>com.MyClass.getName</ignore>
                        </ignores>

But seem like it doesn't work.

I found this link: http://jira.codehaus.org/browse/MCOBERTURA-52 Looks like this problem is about 5 year old. Is there any solution of my problem?

Wes answered 22/11, 2011 at 11:16 Comment(0)
M
7

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>
Mousetrap answered 24/3, 2015 at 7:30 Comment(0)
I
0

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?

Illiteracy answered 22/11, 2011 at 14:34 Comment(0)
M
0

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);
    }
}   
Melba answered 10/11, 2016 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.