2016-03-21 59 views
0

我不明白爲什麼我的測試不起作用。我嘗試攔截並跳過發送到作爲bean引用的端點,並沒有任何反應。 我正在使用版本2.16.2。阿帕奇駱駝。 InterceptSendTo不適用於豆端點

測試camel.xml

<bean id="eb" class="com.rencap.emf.bpipe.EndpointBean"/>  
<camel:camelContext xmlns="http://camel.apache.org/schema/spring"> 
    <camel:endpoint id="requestEP" uri="direct:request"/> 
    <endpoint id="beanEP" uri="bean:eb?method=processMessage" /> 
    <camel:route id="testRoute"> 
     <camel:from ref="requestEP"/> 
     <camel:to ref="beanEP" /> 
    </camel:route> 
</camel:camelContext> 

EndpointBean.java

package com.rencap.emf.bpipe; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

public class EndpointBean { 
    private static Logger LOG = LoggerFactory.getLogger(EndpointBean.class); 

    public void processMessage(String msg){ 
     LOG.info("Processing message: {} ",msg); 
    } 
} 

單元測試:

@EndpointInject(ref="requestEP") 
ProducerTemplate requestEP; 
@EndpointInject(ref="beanEP") 
ProducerTemplate beanEP; 
@Autowired 
ModelCamelContext camelContext; 
@Test 
public void test() throws Exception{ 

    camelContext.getRouteDefinition("testRoute").adviceWith(camelContext , new AdviceWithRouteBuilder(){ 
     @Override 
     public void configure() throws Exception { 
      interceptSendToEndpoint(beanEP.getDefaultEndpoint().getEndpointUri()). 
       to("mock:send"). 
       skipSendToOriginalEndpoint(); 
     } 
    });  
    TestUtils.waitingFor("Configuration applied", 2000); 

    MockEndpoint mockEP = camelContext.getEndpoint("mock:send",MockEndpoint.class); 
    mockEP.setExpectedCount(1); 

    requestEP.sendBody("Message"); 

    mockEP.assertIsSatisfied(); 

    TestUtils.waitingFor("All rows commited", 2000); 
} 

測試總是失敗。 日誌:

13:11:02.512 [main] INFO o.apache.camel.model.RouteDefinition - AdviceWith route after: Route(testRoute)[[From[ref:requestEP]] -> [InterceptSendToEndpoint[bean://eb?method=processMessage -> [To[mock:send]]], To[ref:beanEP]]] 
13:11:02.537 [main] INFO o.a.camel.spring.SpringCamelContext - Route: testRoute started and consuming from: Endpoint[direct://request] 
13:11:02.538 [main] INFO com.rencap.emf.bpipe.test.TestUtils - Wait 2000 ms. Configuration applied 
13:11:04.554 [main] INFO com.rencap.emf.bpipe.EndpointBean - Processing message: Message 
13:11:04.556 [main] INFO o.a.c.component.mock.MockEndpoint - Asserting: Endpoint[mock://send] is satisfied 

這意味着,發送到端點沒有被攔截和跳過。可能是我不明白的東西,但我沒有發現使用這種方法的任何限制。

另外我注意到與log相同的問題。如果我更換beanEP:

<endpoint id="beanEP" uri="log:LOGMESSAGE" /> 

我收到相同的結果。 但如果我取代它

<endpoint id="beanEP" uri="seda:send" /> 

,並添加新的路由:

<camel:route id="route2"> 
     <camel:from ref="sendEP"/> 
     <camel:log message="msg received ${body}"/> 
    </camel:route> 

,我會得到預期的結果和測試將successed。

我該怎麼做?或者也許這種方法有一些限制嗎?

回答

0

我被建議覆蓋isUseAdviceWith。在我的情況下它看起來像:

public class TestTest extends CamelSpringTestSupport { 
    @Test 
    public void test() throws Exception{ 
     Endpoint beanEP = context.getEndpoint("beanEP"); 
     Endpoint requestEP = context.getEndpoint("requestEP"); 
     context.getRouteDefinition("testRoute").adviceWith(context , new AdviceWithRouteBuilder(){ 
      @Override 
      public void configure() throws Exception { 
       interceptSendToEndpoint(beanEP.getEndpointUri()). 
        to("mock:send"). 
        skipSendToOriginalEndpoint(); 
      } 
     });  
     context.start(); 
     TestUtils.waitingFor("Configuration applied", 2000); 
     MockEndpoint mockEP = context.getEndpoint("mock:send",MockEndpoint.class); 
     mockEP.setExpectedCount(1); 
     context.createProducerTemplate().sendBody(requestEP, "Message"); 
     mockEP.assertIsSatisfied(); 
     TestUtils.waitingFor("All rows commited", 2000); 
    } 
    @Override 
    protected AbstractApplicationContext createApplicationContext() { 
     return new ClassPathXmlApplicationContext("test-camel.xml"); 
    } 
    @Override 
    public boolean isUseAdviceWith(){ 
     return true; 
    } 
} 

它的工作原理。

但我想使用註釋,但this現在還不適用於我。