Spring boot application.yml中的ObjectMapper配置未在测试中使用

Spring boot application.yml中的ObjectMapper配置未在测试中使用,spring-boot,jackson,Spring Boot,Jackson,请注意,这与中的问题不同,后者涉及导入编程配置的ObjectMapper 我想为ObjectMapper的配置提供一个单一的真实来源,而实现这一点的逻辑位置是通过spring.jackson属性的springapplication.yml。但我不知道如何应用该配置。我在JUnit5测试中使用了一个简单的@extendedwith(SpringExtension.class)注释 我尝试了@AutoConfigureJsonTesters,@Import(JacksonAutoConfigurat

请注意,这与中的问题不同,后者涉及导入编程配置的ObjectMapper

我想为ObjectMapper的配置提供一个单一的真实来源,而实现这一点的逻辑位置是通过
spring.jackson
属性的spring
application.yml
。但我不知道如何应用该配置。我在JUnit5测试中使用了一个简单的
@extendedwith(SpringExtension.class)
注释


我尝试了
@AutoConfigureJsonTesters
@Import(JacksonAutoConfiguration.class)
@ContextConfiguration(classes=JacksonAutoConfiguration.class)
,但没有成功。

您可以在测试类中自动连接
ObjectMapper
,如下所示

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

  @Autowired
  private MockMvc mockMvc;

  @Autowired
  private ObjectMapper objectMapper;

  @Autowired
  private UserRepository userRepository;

  @Test
  void testCreate() throws Exception {
    User user = new Usere(“ahhi”, “abhi@gmail.com”);

    mockMvc.perform(post("/register/user")
            .contentType("application/json")
            .param("sendWelcomeMail", "true")
            .content(objectMapper.writeValueAsString(user)))
            .andExpect(status().isOk());

    UserEntity userEntity = userRepository.findByName("ahhi");
    assertThat(userEntity.getEmail()).isEqualTo("abhi@gmail.com");
  }
}

您已经为您的默认应用程序配置了它吗?看起来怎么样?您是否使用过任何自定义配置文件?您是否有用于测试运行时的自定义application.yml配置文件?请添加应用程序的所有相关部分。关键是使用
@SpringBootTest(classes=JacksonAutoConfiguration.class)
,然后使用正确配置的Jackson ObjectMapper。也许你可以修改你的答案。