When we generate unit test coverage reports for a project using Cobertura, we often have the following requirements:
Requirements Description:
Because a project has many interface definitions, constant definitions, exception class definitions, these classes do not require unit testing. And when we use Cobertura to generate test reports, if we do not exclude these classes, it will make the reported data is not very good-looking, because after all, there are some of the classes are included in the corresponding unit test. So we want to be able to automatically exclude these interfaces, constants, and exception classes when generating test reports with Cobertura-maven-plugin.
Workaround:
In fact, it is very simple, as long as the Cobertura-maven-plugin in the project Pom.xml in the <excludes> elements under the <instrumentation> elements of these interfaces, constants, It's OK to rule out exceptions. To this end, we must abide by some of the conventions:
A common exclusion definition is as follows:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactid>cobertura-m
Aven-plugin</artifactid> <version>2.5.1</version> <configuration> <instrumentation> <!--charles:excludes the package name which con Tains "Interfaces", "constants", "Exceptions"--> <!--so now, the UT test coverage s Hould is much better--> <excludes> <exc Lude>com/walmart/platform/io/interfaces/*.class</exclude> <exclude>co M/walmart/platform/io/constants/*.class</exclude> <EXCULDE>COM/WALMART/PL Atform/io/exception/*.class</exculde> </excludes> </inst
Rumentation> </configuration> </plugin>
So we've eliminated a number of paths in the <exclude> here, So let's just do it in the project. All the interface classes are placed in the package path at the beginning of the com.walmart.platform.io.interfaces, all the constant classes are placed in the package path at the beginning of the com.walmart.platform.io.constants, all the exception classes Placed in the package path at the beginning of the com.walmart.platform.io.exception.
When running MVN Cobertura:cobertura, generating test reports automatically excludes these 3 categories.