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.
@Transaction annotation for integration testing in Spring Boot.
The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.
We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.
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.
We use maven-surefire-plugin for unit tests and maven-failsafe-plugin for integration tests. They both integrate nicely with Sonar.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With