views:

1014

answers:

3

I'm trying to configure my WAR project build to fail if the line or branch coverage is below given thresholds. I've been using the configuration provided on page 455 of the excellent book Java Power Tools, but with no success. Here's the relevant snippet of my project's Maven 2 POM:

<build>
...
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <check>
        <!-- Per-class thresholds -->
        <lineRate>80</lineRate>
        <branchRate>80</branchRate>
        <!-- Project-wide thresholds -->
        <totalLineRate>90</totalLineRate>
        <totalBranchRate>90</totalBranchRate>
      </check>
      <executions>
        <execution>
          <goals>
            <goal>clean</goal>
            <goal>check</goal>
          </goals>
        </execution>
        <execution>
          <id>coverage-tests</id>
          <!-- The "verify" phase occurs just before "install" -->
          <phase>verify</phase>
          <goals>
            <goal>clean</goal>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
      <instrumentation>
        <excludes>
      <exclude>au/**/*Constants.*</exclude>
        </excludes>
        <ignores>
      <ignore>au/**/*Constants.*</ignore>
        </ignores>
      </instrumentation>
    </configuration>
  </plugin>
  ...
</plugins>
...
</build>

As I say, the coverage report works fine, the problem is that the "install" goal isn't failing as it should if the line or branch coverage is below my specified thresholds. Does anyone have this working, and if so, what does your POM look like and which version of Cobertura and Maven are you using? I'm using Maven 2.0.9 and Cobertura 2.2.

I've tried Googling and reading the Cobertura docs, but no luck (the latter are sparse to say the least).

+1  A: 

Add the following to the <check/> configuration.

<haltOnFailure>true</haltOnFailure>
bmatthews68
Thanks for answering, but I tried that and it didn't work. From what I've read about that setting, it determines whether the build halts upon a test failure, not whether the build fails (or halts) because of low coverage. Is it working for you, or were you just suggesting something to try?
Andrew Swan
A: 

can we use cobertura without writing test cases???

jrang
You might be able to create Cobertura instrumented application. Then execute that application, and run your manual tests. The you should be able to create coverage reports.
Juha Syrjälä
+3  A: 
Pascal Thivent
I replicated your success using your config, so now I just have to integrate it into my real projects. BTW, there are several differences between my config and yours, e.g. my "executions" tag is a child of my "configuration" tag, whereas yours is a sibling (as in the book; I don't know why I deviated from that).
Andrew Swan
Glad to know. And indeed, configuration are different which is actually a good thing :) I don't know how I missed that.
Pascal Thivent