gpt4 book ai didi

web-services - 如何模拟 WS 客户端并在 Camel route 提供响应

转载 作者:行者123 更新时间:2023-12-04 18:06:39 24 4
gpt4 key购买 nike

我想在 Camel 路由中模拟 WS 客户端调用,检查请求并提供响应。

这是一个测试

package com.example.helloworld;

import org.apache.camel.*;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author deveproject
* @version 9/15/14
*/
public class WsClientMockTest extends CamelSpringTestSupport {

@Produce(uri = "direct:test-ws-client-mock")
protected ProducerTemplate template;

@Override
public String isMockEndpoints() {
return "cxf:bean:greeterService";
}

@Test
public void testGetActionRoute() throws Exception {
MockEndpoint greeterService = getMockEndpoint("mock:cxf:bean:greeterService");

greeterService.expectedMessagesMatches(new Predicate() {

@Override
public boolean matches(Exchange exchange) {
assertEquals("U.S.", exchange.getIn().getBody(String.class));
return true;
}
});
template.sendBody("U.S.");


greeterService.assertIsSatisfied();
}

@Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
}
}

org.apache.camel.Predicate 检查请求是否正确。
我找不到如何提供回复的方法。

我有上述测试的工作示例。我可以按需发布。

谢谢你。

最佳答案

您可以将 returnReplyBody(Expession expression) 方法用于模拟响应。

看例子:

@Test
public void testGetActionRoute() throws Exception {
MockEndpoint greeterService = getMockEndpoint("mock:cxf:bean:greeterService");

greeterService.returnReplyBody(new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
GreeterServiceResponse response = new GreeterServiceResponseResponse();
response.setGreeting("Hello!");
return (T) response;
}
});

greeterService.expectedMessagesMatches(new Predicate() {

@Override
public boolean matches(Exchange exchange) {
assertEquals("U.S.", exchange.getIn().getBody(String.class));
return true;
}
});
template.sendBody("U.S.");


greeterService.assertIsSatisfied();
}

关于web-services - 如何模拟 WS 客户端并在 Camel route 提供响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25856512/

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