title: Testing HttpRequestExecutingMessageHandler using MockRestServiceServer
body: <p>I'm working on an app using Spring Integration that uses the <code>HttpRequestExecutingMessageHandler</code> 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 <code>MockRestServiceServer</code> to do this. However, the <code>MockRestServiceServer</code> doesn't seem to be intercepting the <code>RestTemplate</code> calls, and instead they are going through (to <code>http://example.com/</code>) and raising <code>java.net.ConnectException: Connection refused</code>. Is there a way to force the <code>HttpRequestExecutingMessageHandler</code> to call <code>MockRestServiceServer</code>, or should I rethink this testing strategy?</p>
  
  <p>Configuration for the application:</p>
  
  <pre><code>@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&lt;String&gt; httpRequestTrigger() { return new ExpressionEvaluatingMessageSource&lt;&gt;(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;
      }
  } 
  </code></pre>
  
  <p>(<code>url</code> is defined in <code>application-test.properties</code> to be <code>http://example.com</code> in test and the real URL otherwise)</p>
  
  <p>Test:</p>
  
  <pre><code>@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();
      }
  }
  </code></pre>
  
  <p>Test results:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p><strong>UPDATE</strong>
  Adapted the test code as follows, as per Artem Bilan's comment:</p>
  
  <pre><code>    mockServer.expect(ExpectedCount.once(), MockRestRequestMatchers.requestTo("http://example.com/"))
          .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
          .andRespond(MockRestResponseCreators.withSuccess("example reply", MediaType.TEXT_PLAIN));
  
      Message&lt;?&gt; message = httpReplyChannel.receive(1001);
      assertNotNull(message);
      assertThat(((ResponseEntity&lt;String&gt;) message.getPayload()).getBody(), is("example reply"));
  </code></pre>
  
  <p>Still getting <code>ConnectException</code> and the example reply sent by the <code>MockRestServiceServer</code> does not seem to get through, because the body of the <code>ResponseEntity</code> is null.</p>