But just before we start, we need to provide group names to our test codes. As an example, we'll model these group types as Felicite's early and later life.
package org.lyeung.simpleheart; public interface EarlyLife {}
Below is a sample test class:package org.lyeung.simpleheart; public interface LaterLife {}
public class TestFelicity {
@Category(EarlyLife.class) @Test public void isKnowsTheodore() { System.out.println("isKnowsTheodore"); assertTrue(true); } @Category(EarlyLife.class) @Test public void isKnowsPaul() { System.out.println("isKnowsPaul"); assertTrue(true); } @Category(EarlyLife.class) @Test public void isKnowsVirginie() { System.out.println("isKnowsVirginie"); assertTrue(true); } @Category(LaterLife.class) @Test public void isKnowsFatherColmiche() { System.out.println("isKnowsFatherColmiche"); assertTrue(true); } @Category(LaterLife.class) @Test public void isParrotLouLou() { System.out.println("isParrotLouLou"); assertTrue(true); } }
As you can see from above, we've categorised our test methods by annotating @Category with the desired group types.
To enable early life tests when Maven runs package goal, we need to update the pom file to customise Surefire plugin:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.18.1</version> </dependency> </dependencies> <configuration> <groups>org.lyeung.simpleheart.EarlyLife</groups> </configuration>
</plugin>
To enable later life tests when Maven runs integration test or after, we need another update to Failsafe plugin:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.18.1</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.18.1</version> </dependency> </dependencies> <configuration> <groups>org.lyeung.simpleheart.LaterLife</groups> </configuration> <executions> <execution> <id>failsafe-integration-tests</id> <phase>integration-test</phase> <goals> <goal>integration-test</goal> </goals> <configuration> <includes> <include>**/*.class</include> </includes> </configuration> </execution> </executions> </plugin>
What this means is that when Maven runs package goal, only tests with early life are invoked. When Maven runs integration test goal or after, both early life and later life tests are invoked1.
"From the threshold of the room she saw Virginia, stretched on her back, her hands joined, her mouth open, and her head thrown back under a black cross bending towards her, between motionless curtains, less white than her face."
Below is a snippet log for package goal:
mvn clean package
------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.lyeung.simpleheart.TestFelicity isKnowsVirginie isKnowsPaul isKnowsTheodore Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in org.lyeung.simpleheart.TestFelicity Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
Below is a snippet log for package verify goal:
mvn clean verify ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.lyeung.simpleheart.TestFelicity isKnowsVirginie isKnowsPaul isKnowsTheodore Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - in org.lyeung.simpleheart.TestFelicity Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.lyeung.simpleheart.TestFelicity isKnowsFatherColmiche isParrotLouLou Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - in org.lyeung.simpleheart.TestFelicity Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
1http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html