[[integration-tests]]
= Running Integration Tests

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:

[source,xml,indent=0,subs="verbatim,attributes"]
----
include::example$integration-tests/pom.xml[tags=integration-tests]
----

Such setup can now use the https://maven.apache.org/surefire/maven-failsafe-plugin[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 xref:integration-tests.adoc#integration-tests.examples.jmx-port[the dedicated example].

You could also configure a more advanced setup to skip the integration tests when a specific property has been set, see xref:integration-tests.adoc#integration-tests.examples.skip[the dedicated example].



[[integration-tests.no-starter-parent]]
== Using Failsafe Without Spring Boot's Parent POM

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:

[source,xml,indent=0,subs="verbatim,attributes"]
----
include::example$integration-tests/failsafe-pom.xml[tags=failsafe]
----

include::partial$goals/start.adoc[leveloffset=+1]

include::partial$goals/stop.adoc[leveloffset=+1]



[[integration-tests.examples]]
== Examples



[[integration-tests.examples.random-port]]
=== Random Port for Integration Tests

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 https://www.mojohaus.org/build-helper-maven-plugin[Build Helper Maven Plugin]:

[source,xml,indent=0,subs="verbatim,attributes"]
----
include::example$integration-tests/random-port-pom.xml[tags=random-port]
----

You can now retrieve the `test.server.port` system property in any of your integration test to create a proper `URL` to the server.



[[integration-tests.examples.jmx-port]]
=== Customize JMX port

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:

[source,xml,indent=0,subs="verbatim,attributes"]
----
include::example$integration-tests/customize-jmx-port-pom.xml[tags=customize-jmx-port]
----

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.



[[integration-tests.examples.skip]]
=== Skip Integration Tests

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:

[source,xml,indent=0,subs="verbatim,attributes"]
----
include::example$integration-tests/skip-integration-tests-pom.xml[tags=skip-integration-tests]
----

By default, the integration tests will run but this setup allows you to easily disable them on the command-line as follows:

[source,shell]
----
$ mvn verify -Dskip.it=true
----
