7

I am trying to separate unit and integration tests through maven command.

pom.xml

....

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Fast*</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

This is my integration test

@RunWith(SpringRunner.class)
@SpringBootTest(classes = StudentApplication.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StudentControllerIT {

...

and this is unit test

@RunWith(SpringRunner.class)
@WebMvcTest(value = StudentController.class, secure = false)
public class StudentFastControllerTest {

....

Now when I try to run command mvn test then only StudentFastControllerTest tests are executed but when I run command mvn integration-test or mvn verify both test classes are executed instead of only StudentControllerIT.

user1298426
  • 3,467
  • 15
  • 50
  • 96
  • 2
    Check this topic https://stackoverflow.com/questions/6612344/prevent-unit-tests-in-maven-but-allow-integration-tests – miskender Mar 10 '18 at 18:08

1 Answers1

4

Edit: There are two approaches that worked for me:

1) using maven-failsafe configurations (I got help from to this answer):

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
            <execution>
                <id>it-tests</id>
                <phase>none</phase> 
                <configuration>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                </configuration>
            </execution>
        </executions>
    </plugin>

Note that this execution is i) identified by the word it-tests and ii) has its phase set to none. The latter detaches these tests from the default lifecycle whereas the former enables us to run them on demand using the following command:

mvn failsafe:integration-test@it-tests

It is also important to have the integration tests suffixed with IT and the same way we include them in this <includes> section we do the same to the <exclude> sections of the other plugins such as surefire.


2) using profiles

In the profiles section you add a profile for your integration tests:

            <profile>
            <id>it-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/*IT.java</include>
                            </includes>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

Additionally, add the following to the profile configuration section if you have you integration tests in a separate test source directory, in this case test-integration:

<testSourceDirectory>test/test-integration/java</testSourceDirectory>

Now go back to the plugins section and on your maven-surefire-plugin, exclude the integration tests from mvn test by adding the following to its configuration section:

<excludes>
    <exclude>**/*IT.java</exclude>
</excludes>

Finally, add the following executions to your maven-failsafe-plugin:

<executions>
    <execution>
        <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
        </goals>
    </execution>
</executions>

As a result (at least in my case), the integration tests are not executed during mvn test but when I run:

mvn failsafe:integration-test -Pit-tests

only my integration tests get executed.

I hope this works for you too.

João Matos
  • 6,102
  • 5
  • 41
  • 76