J. Snook
J. Snook

Reputation: 99

Spring Boot Autowiring of JsonDeserializer in Integration test

I have an application which needs a service to be Spring wired in a JsonDeserializer. The problem is that when I start up the application normally it is wired, but when I start it up in a test, it is null.

The relevant code is:

JSON Serializer/Deserializer:

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

Domain Object:

public class BookingLine extends AbstractEntity implements TelEntity {

    .....other fields

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

    ..... other fields
}

Test Class:

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

Any ideas?

Upvotes: 1

Views: 1485

Answers (1)

J. Snook
J. Snook

Reputation: 99

Managed to discover the answer to this one after fiddling around long enough. Just needed some config like this:

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

Upvotes: 1

Related Questions