6
votes

The maven fail-safe plugin needs to be able to tell the difference between unit tests and integration tests. It seems that when using JUnit one way to separate tests is to use JUnit @Categories annotation. This blog post show how to do this using junit http://www.agile-engineering.net/2012/04/unit-and-integration-tests-with-maven.html

@Category(IntegrationTest.class)
public class ExampleIntegrationTest{

 @Test
 public void longRunningServiceTest() throws Exception {

 }
}

How can I accomplish the same same thing with TestNG and Maven failsafe plugin. I want to use annotations on the test classes to mark them as integration tests.

3
Why would you like to use annotations instead of the naming conventions? Furthermore in most situations it's better having a separate maven module which contains the integration tests. You might use the groups information in TestNG instead but i think it's the wrong way (my opinion).khmarbaise
@khmarbaise I don't want to have a separate integration test modules as it will create an explosion of modules in my app. Annotations make it easy in eclipse to find all classes that are tagged with the integration test annotation. I could define my own annotations that are more specialized such as rest api integration tests that require a running tomcat server, vs selenium tests, vs service integration tests that require a database server but no tomcat server ... etc. In short the annotations are lot more flexible.ams

3 Answers

2
votes

This can be added to the test.

@IfProfileValue(name="test-profile", value="IntegrationTest")
public class PendingChangesITCase extends AbstractControllerIntegrationTest {
    ...
}

To select the tests to be executed just add the value to the profile for executing the integration tests.

<properties>
    <test-profile>IntegrationTest</test-profile>
</properties>

If the maven profile selected does not have the property value, it will not execute the integration tests.

1
votes

We use maven-surefire-plugin for unit tests and maven-failsafe-plugin for integration tests. They both integrate nicely with Sonar.

0
votes

It looks like i've arrived late to this party but for future googlers, I got it to work by doing the following:

Annotate the relevant test classes with a group name of your choice:

@Test(groups='my-integration-tests')
public class ExampleIntegrationTest {
  @Test
  public void someTest() throws Exception {

 }
}

Tell the surefire plugin (which runs the normal unit test phase) to ignore your integration tests:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <excludedGroups>my-integration-tests</excludedGroups>
  </configuration>
</plugin>

And tell the failsafe plugin (which runs the integration test) to only care about your groups.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.20</version>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>**/*.java</includes>
    <groups>my-integration-tests</groups>
  </configuration>
</plugin>