具有模拟端点的骆驼路线测试

时间:2018-08-21 08:36:18

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

使用Springboot,Activemq和模拟端点进行骆驼路由测试

我有一条骆驼路线,该路线从activemq读取并传递给处理器,后者执行进一步的处理和业务逻辑。

我正在尝试通过ProducerTemplate生成和发送消息进行测试,创建模拟端点“ mock:result”,并将其编织为路由的最后一个节点并对其进行断言。不能满足断言。

骆驼路线:

from("queue:myIncomingQueue?username=***&password=***")
    .doTry()
        .log(LoggingLevel.INFO, "Incoming Message: [ body:${body} ]")
        .to("bean-validator:validateIncomingMessage")
        .unmarshal(myUnmarshller.format())
        .setHeader(Constants.MESSAGE_VALID, constant(true))
    .endDoTry()
    .doCatch(Exception.class)
        .log(LoggingLevel.ERROR, 
            "failed to parse message [ body:${body} ], Exception - "
                + exceptionMessage())
    .end()
    .choice()
        .when(header(Constants.MESSAGE_VALID).isNotNull())
            .doTry()
                .process(myProcessor)
            .endDoTry()
            .doCatch(Exception.class)
                .log(LoggingLevel.ERROR, 
                    "failed to process message [ body:${body} ], Exception - "
                        + exceptionMessage())
            .end()
        .endChoice()
    .end();

测试类:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = {Application.class}, properties = { 
    "camel.springboot.java-routes-include-pattern=**/MyRoute*"
})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("mock:result")
public class MyRouteTest {

    @Autowired
    private CamelContext camelContext;

    @Produce(uri="queue:myIncomingQueue?username=***&password=***")
    private ProducerTemplate template;

    @EndpointInject(uri = "mock:result")
    private MockEndpoint mockOutput;

    @Before
    public void setUp() throws Exception
    {
        camelContext.getRouteDefinitions().get(0).adviceWith(camelContext,
            new AdviceWithRouteBuilder()
            {
                @Override
                public void configure() throws Exception {
                    weaveAddLast().to("mock:result");
                }
            });
        camelContext.start();
    }

    @Test
    public void messagesuccessful() throws Exception {
        Exchange dummyExchange = this.generateDataExchange();
        mockOutput.expectedMessageCount(1);
        mockOutput.expectedBodiesReceived(dummyExchange.getIn().getBody());
        mockOutput.message(0).header("API-X-HEADER").isEqualTo(123);

        template.send(dummyExchange);
        Thread.sleep(6000);
        mockOutput.assertIsSatisfied();
    }
}

结果:

> java.lang.AssertionError: mock://result Received message count. Expected: <1> 
but was: <0>
Expected :<1> 
Actual   :<0>
<Click to see difference>

0 个答案:

没有答案