Spring Boot Testing Concepts
Jump to navigation
Jump to search
External
- https://www.baeldung.com/spring-boot-testing
- https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
- https://www.baeldung.com/injecting-mocks-in-spring
- https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing
Internal
Overview
Dependencies
Use:
dependencies {
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
This started dependency pulls everything that is needed to write JUnit tests.
Annotations
Required:
- @SpringBootTest - this is required, otherwise beans won't get injected in our tests.
- @MockBean - automatically injects Mockito mocks.
Optional:
- @WebMvcTest more research
- @RunWith(SpringRunner.class) more research
- @IntegrationTest more research
- @WebIntegrationTest more research
Spring Boot Mockito Support
The Default Generated XyzApplicationTests
Spring Initializr creates a
@SpringBootTest
class XyzApplicationTests {
@Test
void contextLoads() {
}
}
How does this work? How is this useful? Does it use name conventions?
SpringBoot Test Example
@SpringBootTest
public class MyTests {
@Autowired
private SomeComponent someComponentWeWantToTest;
// ...
}
For an example of how to use Mockito mocks, see:
Testing Logging Configuration
By default, test logging is executed by Logback.