java - Camel junit 使用bean组件测试http组件

标签 java junit spring-boot apache-camel

我的 Camel 上下文中有这样的路线:-

<camel:route id="xxx">
 <camel:from uri="direct:test"></camel:from>
 <camel:to uri="bean:testProcessor"></camel:to>
 <camel:to  uri="{{testUrl}}"></camel:to>
 <camel:to uri="bean:responseHandler"></camel:to>
</camel:route>

我想测试整个路由。所以每当我向 direct:test 发送请求时,它都会调用 testProcessor,然后使用 testUrl 调用 http 服务,然后调用 responseHandler bean 元素。我该如何测试这个?最重要的是这里 stub http 服务

最佳答案

我在理解您的确切用例时遇到了一些困难,因此我将向您指出可用于测试的 Camel 的 AdviceWithRouteBuilder 库。我对基于 Camel xml 的版本不是 100% 流畅,所以我将在我的示例中使用 Java DSL。以下是一些 Camel 文档的链接,您可以将其用作引用: http://camel.apache.org/advicewith.html

//示例路线

from("direct:myNormalInput").routeId("xxx")
    .to("myBean", "myMethod").id("enrichmentBean")
    .to("http://myawesomeurl").id("HttpCaller")
    .to("myResponseBean", "myMethod").id("responseHandler");

//示例单元测试

public void myTest throws Exception {
    context.getRouteDefinition("xxx").adviceWith(context, new AdviceWithRouteBuilder() {
        //You can replace your normal route's from statement
        replaceFromWith("direct:testEntry");
        //Swap out your enrichmentBean with a replace or remove it if you prefer
        weaveById("enrichmentBean").replace().to("myTestBean", "myTestMethod");
        //mock out your http call with a different url or a fake endpoint
        weaveById("HttpCaller").replace().to("http://myTestUrl");
        //extract your message at any point in processing to do some validation work
        weaveById("responseHandler").after().to("mock:extract");
    }
    context.start();

    template.sendBody("direct:testEntry", "myTestBody");

    MockEndpoint test = getMockEndpoint("mock:extract");
    int messageCount =  test.getReceivedExchanges().size();
    assertEquals(1, messageCount);
}

关于java - Camel junit 使用bean组件测试http组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290910/

相关文章:

java - 如何创建 ImageIcon 的实例

java - 一旦读取大约 30,000 个邮政编码,程序就会出现滞后

java - 有没有一种自动化的方法来确保代码的所有部分都经过单元测试?

java - Tomcat 作为代理服务器

java - 空指针异常在哪里?

java - Java/COLT 中 "long"的矩阵?

java - 如何对有两个公共(public)方法,一个调用另一个的情况进行单元测试?

java - JUnit 测试 ArrayList 元素不重复

java - 映射通过引用带有注释的未知目标实体属性

spring-boot - Spring Boot Azure AD 自定义角色