Maven and code metrics: check for a test case for each class

Is there anything Maven can use to automate such a check? I see checkstyle and PMD, but I do not find this function.

Basically, I would like the build to fail if there is a class A and there is no ATestCase . I know that this is not a strict check, and it can be easily circumvented by creating a class, but for now this will be enough.

+6
source share
2 answers

What are you looking for

Since Jens Piegsa indicated id, what you are looking for is a tool that shows you test coverage, in other words, the percentage of code that you use for testing.

This allows you to see how much you are testing the code, actually a more reliable way than (at least a class test).

You can use Cobertura, which is well integrated in Maven: http://mojo.codehaus.org/cobertura-maven-plugin/

Way to achieve this

POM configuration

Just put this piece of code in your pom.xml

 <project> ... <reporting> <plugins> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.6</version> </plugin> </plugins> </reporting> </project> 

Coverage launch

And run

  mvn cobertura:cobertura 

Or run the report phase (tied to site generation)

  mvn site:site 

Adding a Quality Threshold

You can even add an error threshold if you want to invalidate low coverage buildings

  <plugin> [...] <configuration> <check> <!-- Fail if code coverage does not respects the goals --> <haltOnFailure>true</haltOnFailure> <!-- Per-class thresholds --> <lineRate>80</lineRate> <!-- Per-branch thresholds (in a if verify that if and else are covered--> <branchRate>80</branchRate> <!-- Project-wide thresholds --> <totalLineRate>90</totalLineRate> <totalBranchRate>90</totalBranchRate> </check> </configuration> </plugin> 
+3
source

Short answer: None.

Longer answer: I once wrote unit test to claim that all VOs had a no-args constructor, and I would have thought that you could use the same approach here.

Basically, iterating through Package.getPackages() (you need to filter out the JRE packages, but assuming you are using a reasonable namespace, this should not be a problem). For each package, collect all classes that do not begin or end with Test , and claim that each of them has a corresponding test.

It's not safe, but close enough, perhaps?

Greetings

+2
source

Source: https://habr.com/ru/post/957887/


All Articles