How to ignore Methods during Code Coverage

Hello again. This is a quick tip for you, hope it helps.

Requirements:

  • Programming Language: Java
  • Project Management: Apache Maven
  • Use the artifact “cobertura-maven-plugin” from the “org.codehaus.mojo” group.

Now with the solution:

  1. Create a simple annotation with any name you like.

public @interface CoverageIgnore() {

}

2. Add the previous annotation to the methods you want to ignore.


@Override
@CoverageIgnore
public User modifyUser(User user) throws DaoException, SecurityException {
  if(user == null) { throw new SecurityException("User null or empty")}

  Userseg userseg;
  List<String> IdProfiles;

  try {
     userseg = userDAO.search(user.getId());
     ...

3. Add the following tags to the plugin in the pom.xml


<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>cobertura-maven-plugin</artifactId>
 <version>2.6</version>
 <configuration>
  <formats>
   <format>xml</format>
  </formats>
  <instrumentation>
   <ignoreTrivial>true</ignoreTrivial>
   <ignoreMethodAnnotation>[packagename].CovertureIgnore</ignoreMethodAnnotation>
  <excludes>
   <include>**/*.class</include>
   <exclude>**/*Test.class</exclude>
   <exclude>**/Test*.class</exclude>
  </excludes>
  </instrumentation>
 </configuration>
</plugin>

4. Compile and you should be set. See you at Twitch, guys.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s