A brief note and reference link for each annotation used in the Spring Boot test Use JUnit 5
build.gradle
testImplementation('org.springframework.boot:spring-boot-starter-test:2.1.5.RELEASE'){
exclude module: 'junit'
}
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
@SpringBootTest Declare when testing or mocking the component registered as a bean in the DI container
--Create a DI container (ʻApplicationContext) --You can't
@Autowiredthe class you want to test without this --If you set the
webEnvironment` attribute, the server will be started when the test is executed.
--By default, it provides a Mock Web environment without starting a server.
46.3 Testing Spring Boot Applications
@WebMvcTest
Declare when testing @ Controller
or @ RestController
--Create a DI container (ʻApplicationContext) only for the
@Controllercomponent --Allows
MockMvc to be
@ Autowired`
46.3.10 Auto-configured Spring MVC Tests
@RestClientTest Declare when testing a component that communicates with Http
--Allows MockRestServiceServer
to be @ Autowired
46.3.20 Auto-configured REST Clients
Recommended Posts