Rowan Jacobs
Rowan Jacobs

Reputation: 389

Testing HttpRequestExecutingMessageHandler using MockRestServiceServer

I'm working on an app using Spring Integration that uses the HttpRequestExecutingMessageHandler class to make backend requests to a REST service at regular intervals. I would like to mock out the REST server in testing, and rather than construct a mock server I would prefer to use MockRestServiceServer to do this. However, the MockRestServiceServer doesn't seem to be intercepting the RestTemplate calls, and instead they are going through (to http://example.com/) and raising java.net.ConnectException: Connection refused. Is there a way to force the HttpRequestExecutingMessageHandler to call MockRestServiceServer, or should I rethink this testing strategy?

Configuration for the application:

@Configuration
public class RestClientTestApplicationConfig {
    @Bean
    @Qualifier("httpRequestChannel")
    public MessageChannel httpRequestChannel() { return new QueueChannel(); }

    @Bean
    @Qualifier("httpReplyChannel")
    public MessageChannel httpReplyChannel() { return new QueueChannel(); }

    @Bean
    public RestTemplate restTemplate() { return new RestTemplate(); }

    @Bean
    @InboundChannelAdapter(value="httpRequestChannel", poller=@Poller(fixedDelay = "1000"))
    public MessageSource<String> httpRequestTrigger() { return new ExpressionEvaluatingMessageSource<>(new LiteralExpression(""), String.class); }

    @Bean
    @ServiceActivator(inputChannel="httpRequestChannel", poller=@Poller(fixedDelay = "1000"))
    public MessageHandler messageHandler(
        RestTemplate restTemplate,
        @Qualifier("httpReplyChannel") MessageChannel messageChannel,
        @Value("${url}") String url
    ) {
        HttpRequestExecutingMessageHandler messageHandler = new HttpRequestExecutingMessageHandler(url, restTemplate);
        messageHandler.setOutputChannel(messageChannel);
        return messageHandler;
    }
} 

(url is defined in application-test.properties to be http://example.com in test and the real URL otherwise)

Test:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RestClientIntegrationTest {
    @Autowired
    private RestTemplate restTemplate;

    private MockRestServiceServer mockServer;

    @Before
    public void setup() {
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void makesBackendRequest() {
        mockServer.expect(ExpectedCount.once(), MockRestRequestMatchers.requestTo("http://example.com/"))
            .andExpect(MockRestRequestMatchers.method(HttpMethod.GET));

        mockServer.verify();
    }
}

Test results:

2016-12-29 16:14:36.902 ERROR 16665 --- [ask-scheduler-2] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://example.com]; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://example.com": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:409)

java.lang.AssertionError: Further request(s) expected leaving 1 unsatisfied expectation(s).
0 request(s) executed.
    at org.springframework.test.web.client.AbstractRequestExpectationManager.verify(AbstractRequestExpectationManager.java:103)
    at org.springframework.test.web.client.MockRestServiceServer.verify(MockRestServiceServer.java:117)
    at com.restclienttest.RestClientIntegrationTest.makesBackendRequest(RestClientIntegrationTest.java:35)

UPDATE Adapted the test code as follows, as per Artem Bilan's comment:

    mockServer.expect(ExpectedCount.once(), MockRestRequestMatchers.requestTo("http://example.com/"))
        .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
        .andRespond(MockRestResponseCreators.withSuccess("example reply", MediaType.TEXT_PLAIN));

    Message<?> message = httpReplyChannel.receive(1001);
    assertNotNull(message);
    assertThat(((ResponseEntity<String>) message.getPayload()).getBody(), is("example reply"));

Still getting ConnectException and the example reply sent by the MockRestServiceServer does not seem to get through, because the body of the ResponseEntity is null.

Upvotes: 4

Views: 4394

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

I think you're good here. Only the problem that you have missed the fact that your application is async. The @InboundChannelAdapter periodically sends messages to the QueueChannel and so on. But it does that in the poller's thread, not that one where you are waiting for verification.

As a fix I think you should really wait for reply in the httpReplyChannel via its .receive(10000) method. And only after that call the mockServer.verify().

UPDATE

Hm. I'd say we have a test-case for you already:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

<int-http:outbound-gateway url="/testApps/httpMethod"
                           request-channel="requestChannel"
                           reply-channel="replyChannel"
                           rest-template="restTemplate"
                           expected-response-type="java.lang.String"
                           http-method-expression="payload"/>

<int:channel id="replyChannel">
    <int:queue/>
</int:channel>

@Autowired
private RestTemplate restTemplate;

private MockRestServiceServer mockServer;

@Before
public void setup() {
    this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}

@Test
public void testDefaultMethod() throws Exception {
    this.mockServer.expect(requestTo("/testApps/httpMethod"))
            .andExpect(method(HttpMethod.POST))
            .andRespond(withSuccess(HttpMethod.POST.name(), MediaType.TEXT_PLAIN));

    this.defaultChannel.send(new GenericMessage<String>("Hello"));
    Message<?> message = this.replyChannel.receive(5000);
    assertNotNull(message);
    assertEquals("POST", message.getPayload());

    this.mockServer.verify();
}

https://github.com/spring-projects/spring-integration/blob/master/spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundGatewayWithMethodExpressionTests.java

Upvotes: 2

Related Questions