I am working on a spring boot project. However, when @WebMvcTest
annotation is used, there is no problem in objectmapper, but when using @AutoConfigureMockMvc
, an error occurs in objectmapper. What's the problem?
@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
class EventControllerTest {
@Autowired MockMvc mockMvc;
@Autowired ObjectMapper objectMapper;
@Test
void createEVent() throws Exception {
Event event = Event.builder()
.id(100)
.name("Spring")
.description("REST API Development with Spring")
.beginEventDateTime(LocalDateTime.of(2021, 10, 30, 12, 00))
.closeEnrollmentDateTime(LocalDateTime.of(2021, 10, 31, 12, 00))
.beginEnrollmentDateTime(LocalDateTime.of(2021, 11, 01, 12, 00))
.endEventDateTime(LocalDateTime.of(2021, 11, 02, 12, 00))
.basePrice(100)
.maxPrice(200)
.limitOfEnrollment(100)
.location("Tokyo")
.free(true)
.offline(false)
.build();
mockMvc.perform(post("/api/event")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaTypes.HAL_JSON)
.content(objectMapper.writeValueAsString(event)))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("id").exists())
.andExpect(header().exists(HttpHeaders.LOCATION))
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE))
.andExpect(jsonPath("id").value(Matchers.not(100)))
.andExpect(jsonPath("free").value(Matchers.not(true)));
}
}