40-Spring-Boot-Testing-and-MockMVC Flashcards
What is the jar file that the MVC testing framework is located?
spring-test.jar
What’re the targets of @MockBean? Example for each
Type and field Field: @MockBean UserService userService; Type: @MockBean(UserService.class) @MockBean(CustomerService.class) class TestClass { @Autowired UserService userService; // ... }
Annotations provided by Spring Boot Testing framework?
- @SpringBootTest
- @WebMvcTest, @WebFluxTest
- @DataJpaTest, @DataJdbcTest, @JdbcTest
- @DataMongoTest, @DataRedisTest
- @MockBean
Starter for Spring boot test
spring-boot-starter-test
List of testing dependencies (not not of jar file) in Spring boot test
- JUnit
- Spring Test & Spring Boot Test
- AssertJ
- Hamcrest
- Mockito
- JSONAssert
- JsonPath - XPath for JSON
What happens when the test class is annotated with @SpringBootTest?
Happen:
1. Search for @SpringBootConfiguration and start application-context
2. Auto-configure TestRestTemplate
@SpringBootApplication is annotated with @SpringBootConfiguration
What are webEnvironment modes in SpringBootTest?
RANDOM_PORT
DEFINED_PORT
MOCK
NONE
What happens when setting webEnviroment to MOCK?
Message sending via TestRestTemplate will send directly to DispatcherServlet
What are the benefits of using TestRestTemplate?
- Relative paths
- Fault-tolerant: does not throw an exception if server return error HTTP status
- Configured to ignore cookies and redirects
How to get the current port when webEnvironment is set to RANDOM_PORT?
- @LocalServerPort
2. @Value(“${local.server.port}”)
In TestRestTemplate, what methods do not return a result?
- delete
2. put
What are TestRestTemplate.{*ForEntity, *ForObject, *ForLocation}?
- ForEntity: return ResponseEntity from here we can get response status
- ForObject: return result. Null if not successful
- ForLocation: return location of newly created object
Annotations inside @WebMvcTest
@SpringBootTest(webEnvironment=MOCK)
@AutoConfigureMockMvc
What @WebMvcTest do?
- Disable full auto-configuration and apply only relevant configurations to MVC tests
- Auto configure MockMvc
- Optionally Spring security
What are 2 classes that support fluid builders when using MockMvc?
MockMvcRequestBuilders
MockMvcRequestMatchers