@SpringBootTest Annotation Example

In this tutorial, you will learn about @SpringBootTest annotation. You will use this annotation 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 scan your application classes and look for different annotations. Depending on which slice of your application you are testing, Spring Framework will create the required beans and add them to 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 random port number. Let’s see how it works.

Read the following tutorial to learn about the difference between @SpringBootTest and @WebMvcTest.

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, 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 configure your SpringBootTest to use WebEnvironment.MOCK.

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

... 

}

Or you will 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 defined in the application.properties file. This is 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 beginners, and you do not need to have prior knowledge of testing Java applications to enrol.

Ready to level up your testing capabilities for Spring Boot applications? Explore our comprehensive tutorials on testing Spring Boot applications available on the Testing Java Code page. Discover best practices, tips, and techniques to ensure your Spring Boot applications are thoroughly tested and dependable.

Happy learning!