java - Camel Spring Boot CXF 端点测试

标签 java spring-boot apache-camel cxf camel-test

我有以下端点和路线。

  @Bean
  public CxfEndpoint requestEndpoint() {
    CxfEndpoint endpoint = new CxfEndpoint();
    endpoint.setAddress(SERVICE_ADDRESS);
    endpoint.setServiceClass(Service.class);
    endpoint.setWsdlURL(WSDL_LOCATION);
    endpoint.setBus(bus);
    endpoint.setProperties(endpointProperties);
    return endpoint;
  }

还有

from("cxf:bean:requestEndpoint")
  //Custom logic with various outbound routes 
  .choice()
  ....

  .to("direct:route1")

  ....

  .to("direct:route2") 

我想测试一下。各种输入数据应路由到不同的路径。

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
@Configuration
public class RequestRouteTest extends CamelTestSupport {

  @Autowired
  private ProducerTemplate producerTemplate;


  @EndpointInject(uri = "mock:direct:route1")
  private MockEndpoint mockCamel;


  @Test
  public void myTest() throws Exception {
    mockCamel.expectedMessageCount(1);

    producerTemplate.sendBody("cxf:bean:requestEndpoint", bodyForRoute1);

    mockCamel.assertIsSatisfied();
  }

} 

但在这种情况下我有以下错误:

Caused by: java.net.ConnectException: ConnectException invoking http://myurl: Connection refused (Connection refused)

这是合乎逻辑的,我没有运行该应用程序。

然后我尝试将 cxf 端点替换为模拟:

MockEndpoint mockEndpoint = getMockEndpoint("mock:cxf:bean:requestEndpoint");
producerTemplate.sendBody(mockEndpoint, bodyForRoute1);

我得到了

Asserting: mock://direct:route1 is satisfied - FAILED

和异常(java.lang.AssertionError:mock://direct:route1 收到的消息计数。预期:<1> 但实际是:<0> ),因为我的路由代码没有被调用。

如何正确测试路由?我想尝试两种有趣的方法:

1) 使用真实的 http 端点进行测试(这允许您测试请求的早期阶段 - 例如 - 具有无效 xml 的请求)

2)当POJO负载位于消息正文中时进行隔离测试。

如果我的问题有解决方案,我将不胜感激

最佳答案

您问题中的路由测试使用 Camel test kit 。这是一个很好的工具,可以为您的 Camel 路线进行“单元测试”,即您的问题#2。

在这些测试中,您通常使用 AdviceWith 用模拟替换真实端点,因为您想测试消息的正确路由

请参阅 @Bedlas 评论中的链接答案,将 CXF 端点替换为直接端点,以使测试正常进行。

如果您想使用真实端点进行测试,即您的问题中的#1,您应该考虑使用像 Citrus 这样的集成测试框架。 。

使用此类框架,您可以针对正在运行的应用程序实例编写测试。在您的情况下,您将针对正在运行的应用程序的真实 CXF 端点发送 HTTP 或 SOAP 请求,并且您有很多可能性来验证结果(检查 JMS 队列、数据库条目等),具体取决于您的应用程序的功能。

关于java - Camel Spring Boot CXF 端点测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772907/

相关文章:

java - Apache Camel - 简单扫描目录和ftp上传

apache-camel - 使用 Apache Camel AWS-KINESIS 端点,如何检查 Kinesis 流中的消息?

java - Android无法在外部存储上创建新目录

spring - 如何配置 Spring Boot 以使用 AWS Cognito (OAuth2/OIDC) 对 Web 应用程序用户和 REST 客户端进行身份验证

java - 序列化/反序列化公共(public)枚举的简单方法?

java - 使用 spring boot 和 activiti 时找不到进程文件夹

java - Spring Boot 如何减少样板代码?

java - 在 Camel 路由中访问 HTTPSession

java - 错误: incompatible types return scores; required: int found: ArrayList<Integer>

java - 在 Java 中实现 C 风格的位域