gpt4 book ai didi

java - 使用 MockRestServiceServer 测试 HttpRequestExecutingMessageHandler

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:52 27 4
gpt4 key购买 nike

我正在使用 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"));

仍然收到 ConnectException 并且 MockRestServiceServer 发送的示例回复似乎没有通过,因为 ResponseEntity 的主体为 null .

最佳答案

我觉得你在这里很好。唯一的问题是您忽略了您的应用程序是异步 的事实。 @InboundChannelAdapter 定期向QueueChannel 等发送消息。但它在轮询器的线程中执行此操作,而不是在您等待验证的线程中执行此操作。

作为修复,我认为您真的应该通过 .receive(10000) 方法在 httpReplyChannel 中等待回复。只有在那之后调用 mockServer.verify()

更新

嗯。我会说我们已经为您准备了一个测试用例:

<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

关于java - 使用 MockRestServiceServer 测试 HttpRequestExecutingMessageHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41389013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com