Использование RestTemplateBuilder с @ContextConfiguration в ИТ-тестах
У меня возникают проблемы при использовании RestTemplateBuilder с @ContextConfiguration в загрузочном приложении Spring (я безуспешно пытался добавить аннотации @SpringBootTest, @RunWith(SpringRunner.class)).
Любая помощь приветствуется. Вот фон:
Я аннотировал свой класс следующим образом:
@ContextConfiguration(classes = {
JsonNodeList.class,
JsonNodeUtils.class,
MyService.class,
RestClient.class,
RestTemplateBuilder.class}, loader = SpringBootContextLoader.class)
public class StepsDefinition {
Класс RestClient имеет RestTemplateBuilder, автоматически подключенный как:
@Autowired
public RestClient(final RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = configureRestTemplate(restTemplateBuilder);
}
Класс MyService автоматически связывает RestClient. Когда я пытаюсь загрузить приложение, используя @ContextConfiguration
с SpringBootContextLoader
я получаю следующую ошибку:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplateBuilder': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.client.RestTemplateBuilder]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.boot.web.client.RestTemplateBuilder.<init>()
1 ответ
Я решил это, используя @SpringBootTest
и добавление RestTemplateAutoConfiguration.class
в массив классов:
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { RestTemplateAutoConfiguration.class })
public class MyTest {
// test methods
}
У меня была такая же проблема при попытке запустить модульные тесты. Вот аннотации, которые работают для меня:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { RestTemplate.class })
@DataJpaTest
@AutoConfigurationPackage
public class MyTest {
// test methods
}
Когда я имел RestTemplateBuilder.class
указано в @SpringBootTest
аннотация:
@SpringBootTest(classes = { RestTemplate.class, RestTemplateBuilder.class })
Я получил то же исключение:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplateBuilder': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.client.RestTemplateBuilder]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.boot.web.client.RestTemplateBuilder.<init>()