非常简单的路线:
import org.apache.camel.builder.RouteBuilder
class TestRoutes extends RouteBuilder {
@Override
void configure(){
from("direct:foo").to("mock:bar")
}
}
非常简单的测试用例:
package mocktest
import static org.junit.Assert.*
import org.junit.*
import org.apache.camel.CamelContext
import org.apache.camel.ProducerTemplate
import org.apache.camel.test.junit4.CamelTestSupport
class TestRouteTests extends CamelTestSupport {
def CamelContext camelContext
def ProducerTemplate producerTemplate
@Test
void testSomething() {
getMockEndpoint('mock:bar').expectedMessageCount(1)
producerTemplate.sendBody('direct:foo', "Hello World")
assertMockEndpointsSatisfied()
}
}
获取此信息:
| Failure: testSomething(mocktest.TestRouteTests)
| java.lang.AssertionError: mock://bar Received message count. Expected: <1> but was: <0>
与骆驼一起尝试:2.12.1,骆驼:2.11.1 今天试图解决问题。完整的测试grails项目仅包含此问题:https://github.com/gutsal-arsen/mocktest
有人可以帮我解决一下吗?
更新1
@Test
void testSimpleRoute() {
def mockEndpoint
mockEndpoint = camelContext.getEndpoint('mock:bar')
//mockEndpoint = getMockEndpoint('mock:bar')
mockEndpoint.expectedMessageCount(1)
producerTemplate.sendBody('direct:foo', 'Hello World')
mockEndpoint.assertIsSatisfied()
}
这个没有:
@Test
void testSimpleRoute() {
def mockEndpoint
//mockEndpoint = camelContext.getEndpoint('mock:bar')
mockEndpoint = getMockEndpoint('mock:bar')
mockEndpoint.expectedMessageCount(1)
producerTemplate.sendBody('direct:foo', 'Hello World')
mockEndpoint.assertIsSatisfied()
}
所以问题出在getMockEndpoint()方法中。
答案 0 :(得分:2)
要解决此问题,我们应该覆盖CamelTestSupport#createCamelContext
方法,因为默认实现创建了单独的camel上下文,它不包含我们的路由(因此,模拟端点是假的)。
像这样:
protected CamelContext createCamelContext() throws Exception {
return camelContext;
}
完整的资源现在看起来像:
package mocktest
import static org.junit.Assert.*
import org.junit.*
import org.apache.camel.CamelContext
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.ProducerTemplate
import org.apache.camel.test.junit4.CamelTestSupport
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.processor.interceptor.Tracer
import org.apache.camel.processor.interceptor.DefaultTraceFormatter
import org.apache.camel.LoggingLevel
class TestRouteTests extends CamelTestSupport {
def CamelContext camelContext
def ProducerTemplate producerTemplate
protected CamelContext createCamelContext() throws Exception {
return camelContext;
}
@Test
void testSomething() {
def mockEndpoint
//mockEndpoint = camelContext.getEndpoint('mock:bar') // this works
mockEndpoint = getMockEndpoint('mock:bar') // this works now also
mockEndpoint.expectedMessageCount(1)
producerTemplate.sendBody('direct:foo', "Hello World")
assertMockEndpointsSatisfied()
}
}
答案 1 :(得分:0)
你从未定义过向mock:bar
端点发送消息的路由......添加类似这样的内容
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:foo").to("mock:bar");
}
};
}