While you may start your Spring Boot application very easily from your test (or test suite) itself, it may be desirable to handle that in the build itself.
To make sure that the lifecycle of your Spring Boot application is properly managed around your integration tests, you can use the start
and stop
goals, as shown in the following example:
link:example$integration-tests/pom.xml[role=include]
Such setup can now use the failsafe-plugin to run your integration tests as you would expect.
Note
|
The application is started in a separate process and JMX is used to communicate with the application.
By default, the plugin uses port 9001 .
If you need to configure the JMX port, see the dedicated example.
|
You could also configure a more advanced setup to skip the integration tests when a specific property has been set, see the dedicated example.
Spring Boot’s Parent POM, spring-boot-starter-parent
, configures Failsafe’s <classesDirectory>
to be ${project.build.outputDirectory}
.
Without this configuration, which causes Failsafe to use the compiled classes rather than the repackaged jar, Failsafe cannot load your application’s classes.
If you are not using the parent POM, you should configure Failsafe in the same way, as shown in the following example:
link:example$integration-tests/failsafe-pom.xml[role=include]
One nice feature of the Spring Boot test integration is that it can allocate a free port for the web application.
When the start
goal of the plugin is used, the Spring Boot application is started separately, making it difficult to pass the actual port to the integration test itself.
The example below showcases how you could achieve the same feature using the Build Helper Maven Plugin:
link:example$integration-tests/random-port-pom.xml[role=include]
You can now retrieve the test.server.port
system property in any of your integration test to create a proper URL
to the server.
The jmxPort
property allows to customize the port the plugin uses to communicate with the Spring Boot application.
This example shows how you can customize the port in case 9001
is already used:
link:example$integration-tests/customize-jmx-port-pom.xml[role=include]
Tip
|
If you need to configure the JMX port, make sure to do so in the global configuration as shown above so that it is shared by both goals. |
The skip
property allows to skip the execution of the Spring Boot maven plugin altogether.
This example shows how you can skip integration tests with a command-line property and still make sure that the repackage
goal runs:
link:example$integration-tests/skip-integration-tests-pom.xml[role=include]
By default, the integration tests will run but this setup allows you to easily disable them on the command-line as follows:
$ mvn verify -Dskip.it=true