0

I have configured the necessary Beans in @Configuration class but have not been able to get the RestTemplate injected into my test class for testing.

@Configuration
public class AppConfig {
    @Bean
    public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
        return new ProtobufHttpMessageConverter();
    }
    @Bean
    public RestTemplate restTemplate(ProtobufHttpMessageConverter converter) {
        RestTemplate http2Template = new RestTemplate(new OkHttp3ClientHttpRequestFactory());
        List<HttpMessageConverter<?>> converters = http2Template.getMessageConverters();
        converters.add(converter);
        http2Template.setMessageConverters(converters);
        return http2Template;
    }
}

Test class:

@ExtendWith(SpringExtension.class)
@AutoConfigureWebClient(registerRestTemplate = true)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = {RestTemplate.class, ProtobufHttpMessageConverter.class})
@ActiveProfiles("dev")
public class GRPCRestApiTest {
    @Autowired
    private RestTemplate restTemplate;
    @Test
    public void GetOneCourseUsingRestTemplate() throws IOException {
        assertNotNull(restTemplate, "autowired restTemplate is NULL!");
        ResponseEntity<Course> course = restTemplate.getForEntity(COURSE_URL, Course.class);
        assertResponse(course.toString());
        HttpHeaders headers = course.getHeaders();
    }
}

Any advice and insight is appreciated

Kok How Teh
  • 3,298
  • 6
  • 47
  • 85

1 Answers1

0

The classes attribute of the annotation @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = {RestTemplate.class, ProtobufHttpMessageConverter.class}) takes component classes to load the application context. You should not put in here anything except your main Spring Boot class or leave it empty.

Furthermore @AutoConfigureWebClient(registerRestTemplate = true) as you want to use the bean you configure inside your application (at least that's what I understood from your question).

So your test setup should look like the following:

// @ExtendWith(SpringExtension.class) can be omitted as it is already part of @SpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("dev")
public class GRPCRestApiTest {

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void GetOneCourseUsingRestTemplate() throws IOException {
        assertNotNull(restTemplate, "autowired restTemplate is NULL!");
        ResponseEntity<Course> course = restTemplate.getForEntity(COURSE_URL, Course.class);
        assertResponse(course.toString());
        HttpHeaders headers = course.getHeaders();
    }
}

This should now start your whole Spring Boot context in dev profile and you should have access to all your beans you define inside your production code like AppConfig.

rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • doesn't work means? Can you maybe add a full-stack trace to your question. Maybe the root cause is somewhere else – rieckpil Jun 15 '20 at 06:04
  • What stack trace? There is no crash. `assertNotNull(restTemplate, "autowired restTemplate is NULL!");` fails. – Kok How Teh Jun 15 '20 at 07:35
  • can you try to inject `TestRestTemplate`? If you want to access your application during the test this is auto-configured for your if you start the whole Spring context and the embedded servlet container – rieckpil Jun 15 '20 at 08:28
  • Which package to include for this missing symbol? Does it inject my existing `RestTemplate` bean? – Kok How Teh Jun 15 '20 at 08:50
  • Just tested. I only changed the `@Autowired` from `RestTemplate` to `TestRestTemplate` but doesn't help. Still getting NULL. – Kok How Teh Jun 15 '20 at 08:58