@SpringBootTest Annotation Example

In this tutorial, you will learn about @SpringBootTest annotation. It is this annotation that you will use to write Integration Tests for your Spring Boot application.

@SpringBootTest annotation is used to create a Spring Application Context that will be used during the test. It will make Spring Framework start scanning classes of your application looking for different annotations. Depending on which slice of your application you are testing, Spring Framework will create the required beans and will add them into the application context.

By default, @SpringBootTest annotation will not start the embedded servlet container(server). But you can configure it to start the embedded server on a defined or on a random port number. Let’s see how it works.

Defined Port Number

To make Spring Framework run an embedded server on a defined port number, use the WebEnvironment.DEFINED_PORT option.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class UsersControllerIntegrationTest {
  
...

}

The port number itself, you will define in an application.properties file.

server.port=8082

If I do not define any port number in the application.properties file, then the embedded servlet container will start on a default port number 8080.

Random Port Number

To avoid port number conflicts in the test environment, configure the web environment to use a random port number.

To make Spring Framework run an embedded servlet container on a random port number, you will use the WebEnvironment.RANDOM_PORT property.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UsersControllerIntegrationTest {
  
...

}

This will make the embedded servlet container always start on a random port number and it will allow test cases to run in parallel. Also, when using a random port number, there is no need to define a specific port number in the application.properties file.

Now, if you do need to know which port number your embedded server is running on, you can inject the running port number into the test class.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UsersControllerIntegrationTest {

  @LocalServerPort
  private int localServerPort;
  
  ...

}

The @LocalServerPort annotation will pick up an actual port number on which our embedded servlet container is running.

MOCK Web Environment

By default, @SpringBootTest annotation will not start any server and your application will be tested in a MOCK servlet environment. So to use a mock servlet environment, you will either configure your SpringBootTest to use WebEnvironment.MOCK.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) 
public class UsersControllerIntegrationTest { 

... 

}

Or do not provide any web environment configuration at all.

@SpringBootTest
public class UsersControllerIntegrationTest { 

... 

}

Configuration Properties

Additionally, you can use @SpringBootTest annotation to set a different value to a configuration property that is defined in the application.properties file. This is very useful if you want to test your application with a different configuration.

Let’s assume that you have a configuration property called “databaseName”. In the application.properties file, this configuration property value is “users”. You can use @SpringBootTest annotation to override this value to be different during the test.

@SpringBootTest(properties="databaseName=test")
public class UsersControllerIntegrationTest { 

 ...

}

Another very useful annotation that is used for Integration Testing is @TestPropertySource. This annotation will allow you to load configuration properties from an external file.

Video lessons

I hope this tutorial was of some value to you. If you like video tutorials,  then have a look at my video course called “Testing Java with JUnit and Mockito“. This video course is for absolute beginners and you do not need to have any prior knowledge of testing Java applications to enrol.

Happy learning!