0% found this document useful (0 votes)
90 views

Spring Professional Exam Tutorial v5.0 - Dominik Cebula

@SpringBootTest annotation allows you to test Spring Boot applications by automatically searching for and loading configurations marked with @SpringBootConfiguration. This includes classes annotated with @SpringBootApplication, which is usually the starting point of the application. @SpringBootTest sets up the test environment, properties, and test tools like TestRestTemplate. It works by using SpringBootContextLoader to transform the configuration into an ApplicationContext based on the annotated class found by SpringBootTestContextBootstrapper.

Uploaded by

Renzo Espinoza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Spring Professional Exam Tutorial v5.0 - Dominik Cebula

@SpringBootTest annotation allows you to test Spring Boot applications by automatically searching for and loading configurations marked with @SpringBootConfiguration. This includes classes annotated with @SpringBootApplication, which is usually the starting point of the application. @SpringBootTest sets up the test environment, properties, and test tools like TestRestTemplate. It works by using SpringBootContextLoader to transform the configuration into an ApplicationContext based on the annotated class found by SpringBootTestContextBootstrapper.

Uploaded by

Renzo Espinoza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Spring Professional

Exam Tutorial v5.0


Question 08

Spring Professional Exam Tutorial v5.0 - Dominik Cebula 1


Question 08 - What does @SpringBootTest do? How does it interact with
@SpringBootApplication and @SpringBootConfiguration?
@SpringBootTest annotation provides following features over regular Spring Test Context:
 Automatically searches for @SpringBootConfiguration
 ...unless nested @Configuration is detected or explicit
@SpringBootTest(classes=...) is specified
 In most cases @SpringBootConfiguration is not explicitly used, it is inherited from
@SpringBootApplication used in production code to indicate starting place for
application
 @SpringBootConfiguration is an alternative to standard @Configuration,
advantage is that @SpringBootConfiguration can be found automatically in tests
 Sets default ContextLoader to SpringBootContextLoader
 ...unless one is explicitly specified in @ContextConfiguration(loader=...)
 SpringBootContextLoader is specific ContextLoader that starts tests using
SpringApplication
 Provides Web Environments
 MOCK (default)
 RANDOM_PORT
 DEFINED_PORT
 NONE
 Allows to easily define Environment properties
 properties field of @SpringBootTest annotation can be used to define key=value
pairs that will be added to Environment before tests execution
 Registers TestRestTemplate and WebTestClient
Spring Professional Exam Tutorial v5.0 - Dominik Cebula 2
Question 08 - What does @SpringBootTest do? How does it interact with
@SpringBootApplication and @SpringBootConfiguration?
@SpringBootTest annotation allows you to set following fields:
 classes - annotated classes to use for loading an ApplicationContext
 If not set, @SpringBootTest will automatically search for
@SpringBootConfiguration which is usually inherited from
@SpringBootApplication
 properties - key=value pairs that will be added to Environment before
tests execution
 webEnvironment – one of specified web environment used for web layer
testing:
 MOCK (default)
 RANDOM_PORT
 DEFINED_PORT
 NONE

Spring Professional Exam Tutorial v5.0 - Dominik Cebula 3


Question 08 - What does @SpringBootTest do? How does it interact with
@SpringBootApplication and @SpringBootConfiguration?
@SpringBootTest annotation interacts with @SpringBootApplication
and @SpringBootConfiguration through
SpringBootTestContextBootstrapper and SpringBootContextLoader.

Goal of SpringBootContextLoader is to transform initial


ContextConfiguration to ApplicationContext.
SpringBootContextLoader will get as input class annotated with
@SpringBootConfiguration, which will be located by
SpringBootTestContextBootstrapper.

Algorithm that searches for class annotated with @SpringBootConfiguration


will start at package where test class is located and will scan this package and all
parent packages in search for @SpringBootApplication.

Spring Professional Exam Tutorial v5.0 - Dominik Cebula 4


Question 08 - What does @SpringBootTest do? How does it interact with
@SpringBootApplication and @SpringBootConfiguration?
@SpringBootTest annotation is used in following way:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationServiceIntegrationTest {

...

...
@SpringBootConfiguration
Discovers ...
public @interface SpringBootApplication {
...
@SpringBootApplication
public class Runner { @SpringBootApplication definition
public static void main(String[] args) {
SpringApplication.run(Runner.class, args);
@SpringBootApplication inherits
} @SpringBootConfiguration
}

Spring Professional Exam Tutorial v5.0 - Dominik Cebula 5


Spring Professional Exam Tutorial v5.0 - Dominik Cebula 6

You might also like