Reputation: 864
I am running into problems while using RestTemplateBuilder with @ContextConfiguration in a Spring boot application (I have tried to add @SpringBootTest, @RunWith(SpringRunner.class) annotation without any luck).
Any help is appreciated. Here is the background:
I have annotated my class like the following:
@ContextConfiguration(classes = {
JsonNodeList.class,
JsonNodeUtils.class,
MyService.class,
RestClient.class,
RestTemplateBuilder.class}, loader = SpringBootContextLoader.class)
public class StepsDefinition {
The RestClient class has RestTemplateBuilder autowired as:
@Autowired
public RestClient(final RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = configureRestTemplate(restTemplateBuilder);
}
MyService class autowires the RestClient. When i try to load the application using @ContextConfiguration
with SpringBootContextLoader
, i am getting the following error:
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>()
Upvotes: 7
Views: 2601
Reputation: 391
I solved this by using @SpringBootTest
and adding RestTemplateAutoConfiguration.class
to the classes array:
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { RestTemplateAutoConfiguration.class })
public class MyTest {
// test methods
}
Upvotes: 7
Reputation: 4802
I had this same issue when attempting to run unit tests. Here are the annotations that are working for me:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { RestTemplate.class })
@DataJpaTest
@AutoConfigurationPackage
public class MyTest {
// test methods
}
When I had the RestTemplateBuilder.class
specified in the @SpringBootTest
annotation:
@SpringBootTest(classes = { RestTemplate.class, RestTemplateBuilder.class })
I got the same exception:
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>()
Upvotes: 0