Testing in Spring Flashcards
What is the role of @SpringBootTest in Spring Boot?
It is used to create an application context for integration testing.
How do you mock a Spring bean in a unit test?
By using @MockBean in conjunction with testing frameworks like Mockito.
What is the difference between @Mock and @MockBean?
@Mock is used with Mockito in unit tests, while @MockBean is used to mock a Spring bean in the application context.
How do you test a Spring MVC controller?
Using MockMvc to simulate HTTP requests and assert responses.
What is @DataJpaTest in Spring Boot?
It is used for testing JPA repositories with an embedded database and limited Spring context.
What is @WebMvcTest used for in Spring Boot testing?
It is used for testing Spring MVC controllers by loading only the web layer of the application.
How does @TestConfiguration help in testing Spring applications?
It allows you to define additional beans or override existing ones specifically for test scenarios.
What is Spring TestContext Framework?
A testing framework in Spring that integrates with JUnit or TestNG to support Spring-based tests.
What is @Transactional used for in testing?
It rolls back transactions after each test method, ensuring a clean database state between tests.
How can you test a Spring Boot REST API?
By using @RestClientTest or MockMvc to mock HTTP requests and validate responses.