Skip to content
Advertisement

RestClientTest annotation fails to autocnfigure RestTemplate

I have a class with RestTemplate dependency

@Service
public class SwiftAPIPushHandler {

    private final ObjectMapper objectMapper;

    private final RestTemplate restTemplate;

    @Value("${app.swift.host}")
    private String swiftHost;

    @Value("${app.swift.client.secret}")
    private String clientSecret;

    public SwiftAPIPushHandler(@Autowired RestTemplate restTemplate,
                                     @Autowired ObjectMapper objectMapper) {
        this.restTemplate = restTemplate;
        this.objectMapper = objectMapper;
    }

    @ServiceActivator
    public Map<String, Object> outboundSwiftPushHandler(Map<String, Object> payload,
                                                              @Header("X-UPSTREAM-WEBHOOK-SOURCE") String projectId) throws JsonProcessingException {
        // HTTP POST Request from RestTemplate here 
    }
}

And in the test I wish to use @RestClientTest for auto configuring the RestTemplate

@RestClientTest
@SpringJUnitConfig(classes = {SwiftAPIPushHandler.class})
public class SwiftAPIPushHandlerTest {

    @Autowired
    SwiftAPIPushHandler apiPushHandler;

    @Test
    public void testSwiftApiPush(
            @Value("classpath:sk-payloads/success-response.json") Resource skPayload) throws IOException {
                // blah blah blah
            }
}

But the test fails with unable to find autowiring candidate for RestTemplate error.

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.swift.cloud.transformation.engine.SwiftAPIPushHandler required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

Advertisement

Answer

In the documentation of @RestClientTest you can read:

If you are testing a bean that doesn’t use RestTemplateBuilder but instead injects a RestTemplate directly, you can add @AutoConfigureWebClient(registerRestTemplate = true).

So if you add to your test class @AutoConfigureWebClient(registerRestTemplate = true) it should properly inject the restTemplate.

@RestClientTest
@AutoConfigureWebClient(registerRestTemplate = true)
@SpringJUnitConfig(classes = {SwiftAPIPushHandler.class})
public class SwiftAPIPushHandlerTest {

The alternative is injecting a RestTemplateBuilder in your service, in this case you don’t need the @AutoConfigureWebClient annotation in your test:

@Autowired
public SwiftAPIPushHandler(RestTemplateBuilder restTemplateBuilder, ObjectMapper objectMapper) {
    this.restTemplate = restTemplateBuilder.build();
    this.objectMapper = objectMapper;
}

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement