@DirtiesContext @Test void testProcessWhichDirtiesAppCtx() { // some logic that results in the Spring container being dirtied } Dirty the context after the current test method.
Question 2
Which two statements are true regarding Spring and Spring Boot Testing? (Choose two.)
Options:
A.
EasyMock is supported out of the box.
B.
@SpringBootTest or @SpringJUnitConfig can be used for creating an ApplicationContext.
C.
Mockito spy is not supported in Spring Boot testing by default.
D.
The spring-test dependency provides annotations such as @Mock and @MockBean.
E.
Integration and slice testing are both supported.
Answer:
B, E
Explanation:
Spring and Spring Boot provide various annotations and utilities to support testing of different layers of the application. To create an ApplicationContext for integration tests, we can use either @SpringBootTest or @SpringJUnitConfig annotations. The former is a specialized annotation that also provides additional features such as auto-configuration, web environment emulation, and test properties management. The latter is a general-purpose annotation that combines @ContextConfiguration and @ExtendWith(SpringExtension.class) annotations.
To perform slice testing, which focuses on testing a specific layer or feature of the application in isolation, we can use annotations such as @WebMvcTest, @DataJpaTest, @RestClientTest, etc. These annotations will only load the relevant beans for the tested slice and mock or stub the other dependencies.