40-Spring-Boot-Testing-and-MockMVC Flashcards

1
Q

What is the jar file that the MVC testing framework is located?

A

spring-test.jar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’re the targets of @MockBean? Example for each

A
Type and field
Field: @MockBean UserService userService;
Type:
@MockBean(UserService.class)
@MockBean(CustomerService.class)
class TestClass {
    @Autowired UserService userService;
    // ...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Annotations provided by Spring Boot Testing framework?

A
  1. @SpringBootTest
  2. @WebMvcTest, @WebFluxTest
  3. @DataJpaTest, @DataJdbcTest, @JdbcTest
  4. @DataMongoTest, @DataRedisTest
  5. @MockBean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Starter for Spring boot test

A

spring-boot-starter-test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List of testing dependencies (not not of jar file) in Spring boot test

A
  1. JUnit
  2. Spring Test & Spring Boot Test
  3. AssertJ
  4. Hamcrest
  5. Mockito
  6. JSONAssert
  7. JsonPath - XPath for JSON
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens when the test class is annotated with @SpringBootTest?

A

Happen:
1. Search for @SpringBootConfiguration and start application-context
2. Auto-configure TestRestTemplate
@SpringBootApplication is annotated with @SpringBootConfiguration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are webEnvironment modes in SpringBootTest?

A

RANDOM_PORT
DEFINED_PORT
MOCK
NONE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when setting webEnviroment to MOCK?

A

Message sending via TestRestTemplate will send directly to DispatcherServlet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the benefits of using TestRestTemplate?

A
  1. Relative paths
  2. Fault-tolerant: does not throw an exception if server return error HTTP status
  3. Configured to ignore cookies and redirects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to get the current port when webEnvironment is set to RANDOM_PORT?

A
  1. @LocalServerPort

2. @Value(“${local.server.port}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In TestRestTemplate, what methods do not return a result?

A
  1. delete

2. put

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are TestRestTemplate.{*ForEntity, *ForObject, *ForLocation}?

A
  • ForEntity: return ResponseEntity from here we can get response status
  • ForObject: return result. Null if not successful
  • ForLocation: return location of newly created object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Annotations inside @WebMvcTest

A

@SpringBootTest(webEnvironment=MOCK)

@AutoConfigureMockMvc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What @WebMvcTest do?

A
  1. Disable full auto-configuration and apply only relevant configurations to MVC tests
  2. Auto configure MockMvc
  3. Optionally Spring security
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are 2 classes that support fluid builders when using MockMvc?

A

MockMvcRequestBuilders

MockMvcRequestMatchers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Get and expect 200 and the field “name” having value “David” with MockMvc

A

mockMvc.perform(get(“…”))
.andExpect(status().isOk())
.andExpect(jsonPath(“name”).value(“David”));

17
Q

What @DataJpaTest do?

A
  1. Load @Repository beans and ignore other components
  2. Auto configure TestEntityManager
  3. Use embedded in-memory database
18
Q

How to override auto-configured database (in-memory DB) in tests?

A

Using @ AutoConfigureTestDatabase

19
Q

What view templates are supported by Spring?

A
FreeMarker
GroovyMarkup
Tiles
Mustache
Thymeleaf
Velocity
GSP (groovy)