java - 集成测试中 JsonDeserializer 的 Spring Boot Autowiring

标签 java spring spring-boot jackson autowired

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

相关代码为:

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
    }
}

有什么想法吗?

最佳答案

经过足够长时间的摆弄后,终于找到了这个问题的答案。只需要一些像这样的配置:

@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);
    }
}

关于java - 集成测试中 JsonDeserializer 的 Spring Boot Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37725028/

相关文章:

java - 在 java 中将对象列表转换为 JSON 数组 - Spring Boot api 测试

java - Hibernate 不会自己创建 Join Table

java - 使用 JSOUP 编写 JSON

java - 包含一个字符串的列表是否等于(在内存使用方面)包含一个字符串和一个空对象的列表?

java - Struts 2 ajax 验证找不到拦截器类 jsonValidationWorkflowStack

spring - 如何在 Spring 3.0.x 中正确测试@Async 邮件程序?

java - Android 从 RSS xml feed 解析 pubDate 标签

spring - 如何从Java EE容器(JBoss)解决依赖关系

java - 通过 Spring/Hibernate 批量插入需要 id 的地方

java - 如何使用 Apache CSV Parser 读取 Excel(.xlsx) Multipartfile?当前方法不适用于设置为 EXCEL 的 CSVFormat