@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE ) public class OrderServiceTest { @Autowired
A Spring Boot application is a Spring
ApplicationContext, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context.![]() |
External properties, logging, and other features of Spring Boot are installed in the context by default only if you use
SpringApplication to create it. |
Spring Boot provides a
@SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication. In addition to@SpringBootTest a number of other annotations are also provided for testing more specific slices of an application.![]() |
If you are using JUnit 4, don’t forget to also add
@RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored. If you are using JUnit 5, there’s no need to add the equivalent @ExtendWith(SpringExtension) as @SpringBootTest and the other @…Test annotations are already annotated with it. |
By default,
@SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run:MOCK(Default) : Loads a webApplicationContextand provides a mock web environment. Embedded servers are not started when using this annotation. If a web environment is not available on your classpath, this mode transparently falls back to creating a regular non-webApplicationContext. It can be used in conjunction with@AutoConfigureMockMvcor@AutoConfigureWebTestClientfor mock-based testing of your web application.RANDOM_PORT: Loads aWebServerApplicationContextand provides a real web environment. Embedded servers are started and listen on a random port.DEFINED_PORT: Loads aWebServerApplicationContextand provides a real web environment. Embedded servers are started and listen on a defined port (from yourapplication.properties) or on the default port of8080.NONE: Loads anApplicationContextby usingSpringApplicationbut does not provide any web environment (mock or otherwise).
![]() |
If your test is
@Transactional, it rolls back the transaction at the end of each test method by default. However, as using this arrangement with either RANDOM_PORT or DEFINED_PORT implicitly provides a real servlet environment, the HTTP client and server run in separate threads and, thus, in separate transactions. Any transaction initiated on the server does not roll back in this case. |
![]() |
@SpringBootTest with webEnvironment = WebEnvironment.RANDOM_PORT will also start the management server on a separate random port if your application uses a different port for the management server. |
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
![[Note]](https://docs.spring.io/spring-boot/docs/current/reference/html/images/note.png)
![[Tip]](https://docs.spring.io/spring-boot/docs/current/reference/html/images/tip.png)
No comments:
Post a Comment