View Source

{info}
The golden rule is that _mvn clean install_ should *ALWAYS* return _Build Successful_.
This means that the test suite which is run by default cannot depend on any external resources like database, JMS, Files, WebServices, etc.
{info}

This does not mean that you cannot have tests that use _in-memory_, embedded alternatives! For now let's assume that we have two types of tests: simple unit tests that only test business and doesn't need any external environment and tests that need external services. Lets look at a concrete example to make things more concrete.

h2. How to set this up

# Annotate the tests in question
{code}
@Test(groups = "external-api")
public void testFetchDataFromExternalAPI() {}
{code}
# Configure surefire (in pom.xml) to exclude these groups of tests
{code}
<properties>
<!--Override this property in a maven profile if you want to run all tests. -->
<excluded.test.groups>external-api</excluded.test.groups>
</properties>

<!-- To run all tests: mvn clean install -Pall-tests -->
<profiles>
<profile>
<id>all-tests</id>
<properties>
<excluded.test.groups/>
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<excludedGroups>${excluded.test.groups}</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.2</version>
<scope>test</scope>
</dependency>
</dependencies>
{code}
# Remember to enable the profile to run all tests in the continuous integration server.
## In Jenkins, Build - Goals and options:
{code}
clean source:jar deploy -Pall-tests
{code}