28 December 2010

Tags: cobertura maven spock testng

Cobertura, TestNG and Maven play well together. However, I had problems when I started migrating some tests to the Spock framework. Long story short, Spock uses JUnit internally, not TestNG, and the surefire plugin only triggers tests for one of those frameworks at a time. Therefore, if you mix TestNG with Spock, you’ll run with problems where your Spock tests won’t be started automatically by Maven, and eventually, Cobertura will never generate test coverage for the corresponding classes.

I have found different proposals for fixing this problem, but only one worked for me, so here’s my working pom.xml :

...

                1.7.6
  0.5-groovy-1.7
  1.2

...



   org.testng
   testng
   5.8
   test
   jdk15


   org.codehaus.groovy
   groovy-all
   ${groovy.version}


     junit
     junit


     org.apache.ant
     ant


     org.apache.ant
     ant-launcher


     jline
     jline





   org.spockframework
   spock-core
   ${spock.version}
   test



   org.codehaus.gmaven.runtime
   gmaven-runtime-1.7
   ${gmaven.version}


     org.codehaus.groovy
     groovy-all



...





    org.codehaus.gmaven
    gmaven-plugin
    ${gmaven.version}



       1.7


       generateStubs
       compile
       generateTestStubs
       testCompile






    org.apache.maven.plugins
    maven-surefire-plugin
    2.7.1


      test

       test


       none:none



      test-testng
      test

       test


       none:none





    org.spockframework
    spock-maven
    ${spock.version}



       find-specs





    org.codehaus.mojo
    cobertura-maven-plugin
    2.4


      cobertura-test
      package

       cobertura



        false




      cobertura-clean
      clean

       clean

The trick is to add two executions of the surefire plugin, as indicated here, but forcing the plugin version to 2.7.1. Also, adding two dependencies to the surefire plugin, as suggested by Kristian Rosenvold didn’t work for me. My pom.xml file also includes the necessary configuration for GMaven to work with Groovy 1.7 instead of 1.6 by default.