很简单的路线:
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() 方法内部。