2016-04-21 72 views
1

我正在寫一個應用程序使用駱駝部署(最終)在保險絲容器中。項目的性質要求我混合並匹配Java和XML DSL。我如何用駱駝模擬藍圖終點?

我無法讓模擬框架與藍圖正常工作。

這是我的單元測試,完全基於示例here

public class MockNotWorking extends CamelBlueprintTestSupport { 

    @Test 
    public void testAdvisedMockEndpointsWithPattern() throws Exception { 

    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { 
     @Override 
     public void configure() throws Exception { 
      mockEndpoints("log*"); 
     } 
    }); 

    getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World"); 
    getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); 

    template.sendBody("direct:start", "Hello World"); 

    // additional test to ensure correct endpoints in registry 
    assertNotNull(context.hasEndpoint("direct:start")); 
    assertNotNull(context.hasEndpoint("log:foo")); 
    assertNotNull(context.hasEndpoint("mock:result")); 
    // only the log:foo endpoint was mocked 
    assertNotNull(context.hasEndpoint("mock:log:foo")); 
    assertNull(context.hasEndpoint("mock:direct:start")); 
    assertNull(context.hasEndpoint("mock:direct:foo")); 

    assertMockEndpointsSatisfied(); 

    } 


    @Override 
    protected RouteBuilder createRouteBuilder() throws Exception { 
    return new RouteBuilder() { 
     @Override 
     public void configure() throws Exception { 
       from("direct:start").to("direct:foo").to("log:foo").to("mock:result"); 

      from("direct:foo").transform(constant("Bye World")); 
     } 
    }; 
    } 

    protected String getBlueprintDescriptor() { 
    return "OSGI-INF/blueprint/blueprint.xml"; 
    } 
} 

我抄逐字的例子here,並修改了它非常輕微,所以我們延長CamelBlueprintTestSupport而不是CamelTestSupport。這需要壓倒一切getBlueprintDescriptor指向我的藍圖XML,我在其中定義了一個非常基本的(和完全無關的測試)路線:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> 

<camelContext id="validationRoute" xmlns="http://camel.apache.org/schema/blueprint" > 

    <route id="validation"> 
     <from uri="direct:validation" /> 
     <log message="validating..." /> 
    </route> 
</camelContext> 

</blueprint> 

測試失敗:

java.lang.AssertionError: mock://log:foo Received message count. Expected: <1> but was: <0> 

所以這意味着消息沒有達到模擬終點。更改CamelBlueprintTestSupportCamelTestSupport,它的工作原理。

那麼,我如何得到像這樣的模擬藍圖正確工作?

回答

0

當您使用藍圖時,它會導入您在藍圖xml文件中定義的所有路線,並將它們添加到CamelContext

這個事情破壞的原因是context.getRouteDefinitions().get(0)不再指唯一的路線 - 現在有多個路線。因此,當您添加AdviceWithRouteBuilder時,可能會將其添加到錯誤的路線中。

下面的代碼解決了這個問題(將非藍圖測試工作太):

List<RouteDefinition> routes = context.getRouteDefinitions(); 
    for (int i=0; i<routes.size(); i++) { 
     context.getRouteDefinitions().get(i).adviceWith(context, new AdviceWithRouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       // mock all endpoints 
       mockEndpoints("log*"); 
      } 
     }); 
    } 

======更新======

一個更好的辦法這樣做不是嘲笑所有路線,而是通過id找到我們的具體路線,然後應用建議。這意味着第一設置路線ID:

from("direct:start").routeId("start").to("direct:foo").to("log:foo").to("mock:result"); 

然後通過ID查找路由和呼叫adviceWith像以前一樣:這取決於

context.getRouteDefinition("start").adviceWith(context, new AdviceWithRouteBuilder() { 
     @Override 
     public void configure() throws Exception { 
      // mock log endpoints 
      mockEndpoints("log*"); 
     } 
    }); 
0

其實什麼版本的駱駝您正在使用,一些方法不按預期工作。如果我記得對的話,它在2.12之後改善得更多。 (2.15是更好的方式)

我所有的遭遇與駱駝單元測試後,我結束了在這裏記錄的所有東西:

http://bushorn.com/unit-testing-apache-camel/

http://bushorn.com/camel-unit-testing-using-mock-endpoint/

順便說一句,而不是這個「mockEndpoints (「log *」);「,我會這樣做:

weaveByToString(」。直接:FOO模擬。 「)後()來(」:日誌「);(這聽起來不可思議,我知道;))

,或者如果你可以設置一個端點ID

weaveById( 「端點-ID-的直接-富」)後()至( 「模擬:日誌」);

weaveAddLast()到(。 「模擬:日誌」);