Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RestTemplateBuilder with @ContextConfiguration in IT tests

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>()
like image 287
aazeem Avatar asked Aug 15 '17 16:08

aazeem


1 Answers

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
}
like image 53
jorritvdven Avatar answered Nov 10 '22 10:11

jorritvdven