Maven and code metrics: check for existence of a test case for each class
Asked Answered
C

2

6

Is there something that can be used in Maven to automate this kind of check? I'm seeing checkstyle and PMD but I'm not finding this feature.

Basically I'd like the build to fail if there's a class A and there's not an ATestCase. I know, it is not a strict check and can be easily bypassed by creating just the class, but at the moment that would be enough.

Coahuila answered 12/11, 2013 at 7:48 Comment(1)
martinfowler.com/bliki/TestCoverage.htmlBernt
L
3

What ou are looking for

As Jens Piegsa pointed id out, what you are looking for is a tool that show you the test coverage, in other words the percentage of code which is used by you tests.

It allow you to see how much you code is tested, in a really more reliable way than (at least test by class).

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

The way to achieve that

POM Configuration

Just put this code snippet 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>

Running coverage

And run

 mvn cobertura:cobertura

Or run the report phase (binded with site generation)

 mvn site:site

Adding quality Threshold

You can even add failing threshold if you want to invalidate low coverage builds

    <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>
Lectionary answered 12/11, 2013 at 14:42 Comment(2)
That's great! Seems work perfectly...but with a little missing thing: <haltOnFailure>true</haltOnFailure>Coahuila
You're right :). I deleted it, and forgot to replace it. Glad to help you :). Il will edit my post, in order to place it.Vermiform
M
2

Short answer: No.

Longer answer: I once wrote a unit test to assert that all VO's had a no-args constructor, and I would think that you could use the same approach here.

Basically, iterate through Package.getPackages() (you'll need to filter out JRE packages, but assuming you're using a sensible namespace, this should be no problem). For each package gather all classes not starting or ending with Test and assert that each one has a matching test.

It's not failsafe, but close enough perhaps?

Cheers,

Mammillate answered 12/11, 2013 at 8:19 Comment(1)
Well, the last time I wanted to get all packages and gather all their classes, I ended up writing a lot of code using some third party library. Java does not provide a reliable way to do this.Bernt

© 2022 - 2024 — McMap. All rights reserved.