模拟在骆驼测试中执行的中间路线的主体

Mock the body of an intermediate route executed in Camel Tests

可用examples of the usage of the Camel Test component显示如何测试期望的路由:

但是我需要做的是模拟中间路线的主体(手动设置),例如:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <bean id="exampleBean" class="xxx.ExampleBean"/>

    <routeContext id="routesTest" xmlns="http://camel.apache.org/schema/spring">

        <route>
            <from uri="direct:route1" />
            <to uri="direct:route2" />
            <log message="${body}"/>
        </route>

        <route>
            <from uri="direct:route2"/>
            <to uri="bean:exampleBean"/>
            <to uri="direct:route3" />
        </route>

        <route>
            <from uri="direct:route3"/>
            <log message="${body}"/>
        </route>

    </routeContext>

</beans>

在这种情况下,我想完全避免 bean:exampleBean 的实际执行,模拟其执行结果。

我的测试class:

public class MyTests extends CamelSpringTestSupport {

    @Produce(uri = "direct:route1")
    protected ProducerTemplate inputProducerTemplate;

    @EndpointInject(uri = "mock:bean:exampleBean")
    protected MockEndpoint mockBeanExampleBean;

    @Test
    public void testRoute() throws Exception {

        CompletableFuture<Object> future = inputProducerTemplate.asyncSendBody("direct:route1", "Some message");
        Object o = future.get();
       
    }

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

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("spring/gesti-test-application-context.xml");
    }

}

public class ExampleBean {

    public String enhance(String message) {
        System.out.println(message);
        //Here I would call a REST API
        return "MY API RESULT";
    }

}

当使用 mockBeanExampleBean.whenAnyExchangeReceived(exchange -> exchange.getMessage().setBody("My message")); 时,它允许覆盖对 exampleBean 的输入,但不会避免它的执行。

在单元测试的上下文中,route2 可能是一个“模拟”组件。一种简洁的实现方法是在属性文件中声明路由。不过,路线的可读性变得更加困难。

然后,您可以:

@EndpointInject("mock://route2")
MockEndpoint mockSecondStep;

mockSecondStep.whenExchangeReceived(1, e -> {
List whatever = new ArrayList<>();
e.getMessage().setBody(whatever);
});

我用 InterceptStrategy:

解决了
public class MyTests extends CamelSpringTestSupport {

    @Test
    public void testRoute() throws Exception {

        CompletableFuture<Object> future = template.asyncSendBody("direct:route1", "Some message");
        Object o = future.get();
        assertEquals("INTERCEPTED!", o);
    }

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("spring/gesti-test-application-context.xml");
    }

        @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                context.getProcessorDefinition("bean:exampleBean").addInterceptStrategy(
                        (context, definition, target, nextTarget) -> exchange -> exchange.getOut().setBody("INTERCEPTED!"));
            }
        };
    }

}

public class ExampleBean {

    public String enhance(String message) {
        System.out.println(message);
        //Here I would call a REST API
        return "MY API RESULT";
    }

}