簡體   English   中英

使用MockRestServiceServer測試HttpRequestExecutingMessageHandler

[英]Testing HttpRequestExecutingMessageHandler using MockRestServiceServer

我正在使用Spring Integration開發一個應用程序,該應用程序使用HttpRequestExecutingMessageHandler類定期向REST服務發出后端請求。 我想在測試中模擬出REST服務器,而不是構建一個模擬服務器,我寧願使用MockRestServiceServer來做這件事。 但是, MockRestServiceServer似乎沒有攔截RestTemplate調用,而是通過(到http://example.com/ )並引發java.net.ConnectException: Connection refused 有沒有辦法強制HttpRequestExecutingMessageHandler調用MockRestServiceServer ,還是應該重新考慮這個測試策略?

應用程序的配置:

@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;
    }
} 

urlapplication-test.properties定義為測試中的http://example.com ,否則為真實URL)

測試:

@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();
    }
}

檢測結果:

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)

更新根據Artem Bilan的評論改編測試代碼如下:

    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"));

仍然獲得ConnectExceptionMockRestServiceServer發送的示例回復似乎沒有通過,因為ResponseEntity的主體為null。

我覺得你在這里很好。 只有您錯過了應用程序異步的問題 所述@InboundChannelAdapter周期性地發送消息到QueueChannel等。 但是它在poller的主題中就是這樣,而不是你在等待驗證的那個。

作為修復,我認為您應該通過其httpReplyChannel .receive(10000)方法等待httpReplyChannel回復。 然后才調用mockServer.verify()

UPDATE

嗯。 我說我們已經有了一個測試用例:

<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM