0

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)));
    }
}
Jaeyeon
  • 25
  • 5

1 Answers1

0

The ObjectMapper is created by Jackson2ObjectMapperBuilder, and you can inject the builder using:

@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
class EventControllerTest {

    @Autowired MockMvc mockMvc;
@Autowired Jackson2ObjectMapperBuilder builder;

    @Test
    void createEVent() throws Exception {
        ObjectMapper objectMapper = builder.build();

        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)));
    }
}
Mehdi Varse
  • 271
  • 1
  • 15
  • 1
    @Medhi Varse Thanks for your help!, But, Could not autowire. No beans of 'Jackson2ObjectMapperBuilder' type found. <- same error occurs. – Jaeyeon Nov 01 '21 at 02:05