使用 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>