I have a custom Jackson config like below:
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializerByType(ZonedDateTime.class,
new ZonedDateTimeSerializer(DateTransformer.ISO_TIMESTAMP_FORMATTER))
.serializerByType(LocalDateTime.class,
new LocalDateTimeSerializer(DateTransformer.ISO_TIMESTAMP_FORMATTER))
.deserializerByType(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTransformer.ISO_TIMESTAMP_FORMATTER));
// .indentOutput(true);
}
}
but when slice testing with @JsonTest, This JacksonConfig is not working(not use my LocalDateTimeDeserializer). But on the Springboot entry class, It shows that JacksonConfig was loaded successfully, and it truely does(when I boot the application, JacksonConfig is working)
But if I manually import this JacksonConfig on my test class, it works again(all tests passed):
@JsonTest
// import manually
@Import(JacksonConfig.class)
public class JsonConfigTest {
@Autowired
private JacksonTester<LocalDate> localDateJacksonTester;
@Autowired
private JacksonTester<LocalDateTime> localDateTimeJacksonTester;
@Test
void shouldSerializeLocalDate() throws IOException {
final LocalDate now = LocalDate.now();
final JsonContent<LocalDate> write = localDateJacksonTester.write(now);
assertThatNoException().isThrownBy(() -> new DateTransformer(write.getJson().replace("\"", "")));
}
@Test
void shouldSerializeLocalDateTime() throws IOException {
final JsonContent<LocalDateTime> write = localDateTimeJacksonTester.write(LocalDateTime.now());
assertThatNoException().isThrownBy(() -> new DateTransformer(write.getJson().replace("\"", "")));
}
}
So I was wondering, is there any chance that @JsonTest can auto load my JacksonConfig ?
Comment From: wilkinsona
No, that's not possible automatically as sliced tests are based on component scanning and your jsonCustomizer bean isn't a component. Please see the following sections of the reference documentation: