0
JsonDeserializer에서 스프링 유선 서비스가 필요한 애플리케이션이 있습니다. 문제는 응용 프로그램을 정상적으로 시작할 때 유선 상태이지만 테스트에서 시작할 때 null입니다.스프링 부트 통합 테스트에서 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
}
}
어떤 아이디어?