Is there still no solution for ignoring setter/get

2020-08-09 08:48发布

问题:

Did someone find a good solution to ignore trivial methods?
Using some automated testing like Unitils is not really an option, since the code-coverage should not go up if only getters/setters are tested!

Using cobertrua-maven-plugin version 2.5.1:
-) ignore for methods does not work <ignore>com.company.*.set*</ignore>
-) did anyone try to include a patch like http://sourceforge.net/tracker/index.php?func=detail&aid=3010530&group_id=130558&atid=720017 into the maven-plugin?
-) anyone uses a different (better?) test-plugin?

A general ignore for every get/set/is* method is also not really a good way, since these patterns can be used in other methods but simle getter/setters. Also it should be easy to guess trivial getters/setters/constructors.

I know about the question: Ignore methods in class. cobertura maven plugin but since it did not get any relevant answers, I thought I give it another shot.

回答1:

Since Cobertura 2.0 there is a switch to exclude trivial methods:

[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.

The cobertura-maven-plugin uses Cobertura 2.x since version 2.6 (see release notes). But i have not yet found a way to pass the switch to the maven plugin configuration.


Although there seems to be some confusion about the state of this feature (see this Jira issue), the flag does seem to work with the following configuration:

<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
  <instrumentation>
    <ignoreTrivial>true</ignoreTrivial>                 
  </instrumentation>
</configuration>


回答2:

The exclusion problem was fixed here:

http://jira.codehaus.org/browse/MCOBERTURA-52

and it's part of the latest release (2.5.2). It's available since 10 Sept. 2012:

http://search.maven.org/#search|gav|1|g%3A%22org.codehaus.mojo%22%20AND%20a%3A%22cobertura-maven-plugin%22