Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute only integration test in spring boot using maven

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.

like image 631
user1298426 Avatar asked Mar 10 '18 15:03

user1298426


People also ask

Does mvn test run integration tests?

The Maven build lifecycle now includes the "integration-test" phase for running integration tests, which are run separately from the unit tests run during the "test" phase. It runs after "package", so if you run "mvn verify", "mvn install", or "mvn deploy", integration tests will be run along the way.

How do I run a single Maven test?

If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”. As the output shows, only the test class we've passed to the test parameter is executed.


1 Answers

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.

like image 67
João Matos Avatar answered Oct 21 '22 07:10

João Matos