【问题标题】:Spring Boot Autowiring of JsonDeserializer in Integration test集成测试中 JsonDeserializer 的 Spring Boot 自动装配
【发布时间】:2016-10-10 01:14:11
【问题描述】:

我有一个应用程序需要在 JsonDeserializer 中使用 Spring 连接的服务。问题是,当我正常启动应用程序时,它是有线的,但是当我在测试中启动它时,它是空的。

相关代码为:

JSON 序列化器/反序列化器:

@Component
public class CountryJsonSupport {

    @Component
    public static class Deserializer extends JsonDeserializer<Country> {

        @Autowired
        private CountryService service;

        @Override
        public Country deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
            return service.getById(jsonParser.getValueAsLong());
        }
    }
}

域对象:

public class BookingLine extends AbstractEntity implements TelEntity {

    .....other fields

    //Hibernate annotations here....
    @JsonDeserialize(using = CountryJsonSupport.Deserializer.class)
    private Country targetingCountry;

    ..... other fields
}

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
@WebIntegrationTest({"server.port=0"})
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class BookingAndLinesControllerFunctionalTest {

    @Test
    public void testGetBooking() {
        Booking booking = bookingRepositoryHelper.createBooking();
        bookingRepository.save(booking);
        String uri = String.format("http://localhost:%s/api/v1/booking-and-lines/" + booking.getBookingCode(), port);
        Booking booking1 = restTemplate.getForObject(uri, Booking.class); // line which falls over because countryService is null
    }
}

有什么想法吗?

【问题讨论】:

    标签: java spring spring-boot jackson autowired


    【解决方案1】:

    在摆弄了足够长的时间后,设法找到了这个问题的答案。只需要一些这样的配置:

    @Configuration
    @Profile("test")
    public class TestConfig {
    
        @Bean
        public HandlerInstantiator handlerInstantiator() {
            return new SpringHandlerInstantiator(applicationContext.getAutowireCapableBeanFactory());
        }
    
        @Bean
        public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(HandlerInstantiator handlerInstantiator) {
            Jackson2ObjectMapperBuilder result = new Jackson2ObjectMapperBuilder();
            result.handlerInstantiator(handlerInstantiator);
            return result;
        }
    
        @Bean
        public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(Jackson2ObjectMapperBuilder objectMapperBuilder) {
            return new MappingJackson2HttpMessageConverter(objectMapperBuilder.build());
        }
    
        @Bean
        public RestTemplate restTemplate(MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter) {
            List<HttpMessageConverter<?>> messageConverterList = new ArrayList<>();
            messageConverterList.add(mappingJackson2HttpMessageConverter);
            return new RestTemplate(messageConverterList);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-15
      • 2016-03-29
      • 1970-01-01
      • 2018-04-15
      • 2021-07-17
      • 2018-08-04
      • 2020-02-09
      • 2021-12-23
      相关资源
      最近更新 更多