3

很简单的路线:

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() 方法内部。

4

2 回答 2

2

为了解决这个问题,我们应该重写CamelTestSupport#createCamelContext方法,因为默认实现创建单独的骆驼上下文,其中不包含我们的路由(因此,模拟端点是假的)。

像这样:

   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()                                                                                             
  }
}
于 2013-10-03T08:29:13.410 回答
0

您从未定义将消息发送到mock:bar端点的路由...添加类似这样的内容

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:foo").to("mock:bar");
        }
    };
}
于 2013-10-01T17:11:35.997 回答