我有这样的路线
from("direct:start").beanRef("someBean");
对于单元测试,我尝试获取它的模拟端点,但不满足expectedMessageCount条件。
MockEndpoint beanMock = getMockEndpoint("mock:bean:someBean");
beanMock.expectedMessageCount(1);
如果我改变路线,一切正常。
from("direct:start").to("bean:someBean");
以下内容也不起作用:
MockEndpoint beanMock = getMockEndpoint("mock:ref:someBean");
如何为beanRef获取正确的模拟端点?
答案 0 :(得分:1)
问题是beanRef
不会产生端点,因此您无法通过getMockEndPoint
访问它。如果要测试bean的结果,可以添加模拟endPoint并测试其内容。
这是一个例子:
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class BeanRefMock extends CamelTestSupport {
public static class SomeBean{
public void handle(Exchange ex){
System.out.println("SomeBean : " +ex);
ex.getIn().setBody(ex.getIn().getBody() +" is Processed By Bean");
}
}
@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext camelContext = super.createCamelContext();
return camelContext;
}
/*
* (non-Javadoc)
*
* @see org.apache.camel.test.junit4.CamelTestSupport#createRouteBuilder()
*/
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").bean(SomeBean.class).to("mock:postBean");
}
};
}
@Test
public void testQuoteCount() throws Exception {
MockEndpoint mockEndpoint = getMockEndpoint("mock:postBean");
mockEndpoint.expectedBodiesReceived("hello mock is Processed By Bean");
template.sendBody("direct:start", "hello mock");
mockEndpoint.assertIsSatisfied();
}
}