As a Java developer there are many instances where you find yourself wanting to separate running unit and integration tests. Here is a simple solution to that problem. The following is a more clear writeup of what is given by JUnit Team.
Step1:
Create a JUnit category.

1
2
3
package com.rushis.test;
public interface IntegrationTest {
}

Step2:
Apply this “marker interface” as a Category to all your integration tests

1
2
3
4
5
6
7
import org.junit.experimental.categories.Category;
import com.rushis.test.IntegrationTest;
 
@Category(IntegrationTest.class)
public class AccountDaoTest extends DaoTest {
    ...
}

Step3:
In your pom.xml, exclude all tests marked by IntegrationTest in Surefire (which runs unit tests), and include all the tests marked by IntegrationTest in Failsafe (which runs integration tests).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.18.1</version>
  <configuration>
    <excludedGroups>com.rushis.test.IntegrationTest</excludedGroups>
  </configuration>
</plugin>
<plugin>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.18.1</version>
  <configuration>
    <includes>
      <include>**/*.java</include>
    </includes>
    <groups>com.rushis.test.IntegrationTest</groups>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions> 
</plugin>

Step4:
To run unit tests you would do a

1
mvn test

To run integration tests you would do a

1
mvn integration-test

or

1
mvn verify

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>