2016-12-10 3 views
0

私ははモックは無視され、エンドポイントはキャメルルートテストではスキップされていない

.bean("xxx") 
.bean("messageParserProcessor") 
.bean("yyy") 

(簡易)テストクラス含むルートのキャメルテストでプロセッサを模擬しようとしています:

public class SomeTest extends CamelTestSupport { 

    @EndpointInject(uri = "mock:messageParserProcessor") 
    protected MockEndpoint messageParserProcessorMock; 

    // Other declarations 

    @Override 
    public boolean isUseAdviceWith() { 
     return true; 
    } 

    @Before 
    public void setUpContext() throws Exception { 
     context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       interceptSendToEndpoint("messageParserProcessor") 
        .skipSendToOriginalEndpoint() 
        .bean(getMockEndpoint("mock:messageParserProcessor")); // Same as using messageParserProcessorMock 
      } 
     }); 
    } 

    @Test 
    public void testParser() throws Exception { 
     context.start(); 
     String expectedBody = "test"; 
     messageParserProcessorMock.expectedBodiesReceived(expectedBody); 
     ProducerTemplate template = context.createProducerTemplate(); 
     template.sendBody(producerTemplateUri, expectedBody); 
     messageParserProcessorMock.assertIsSatisfied(); 
     context.stop(); 
    } 

    @Override 
    protected JndiRegistry createRegistry() throws Exception { 
     JndiRegistry jndi = super.createRegistry(); 
     jndi.bind("messageParserProcessor", new MessageParserProcessor()); 

     // Other bindings 

     return jndi; 
    } 

    // Other methods 
} 

私はテストを実行し、モックは使用されず、実際のMessageParserProcessorがレジストリから使用されていることがログに表示されます。ここ
は、ログの関連する部分である:私のセットアップで間違っている

Skipping starting CamelContext as isUseAdviceWith is set to true. 
AdviceWith route after: 
Route[[From[aws-sqs://xxx]] -> 
[InterceptSendToEndpoint[messageParserProcessor -> [Bean[mock://messageParserProcessor]]], Bean[ref:messageParserProcessor]]] 
*Here I get logs from the actual processor, which I don't expect* 

何? 私もやってみました:

interceptSendToEndpoint("messageParserProcessor") 
    .skipSendToOriginalEndpoint() 
    .process(new Processor() { 
     @Override 
     public void process(Exchange exchange) throws Exception { 
      // Log and print something 
     } 
    }); 

しかし、何も印刷されない、または記録されます。

また、最初の場所で実際の豆をバインドする必要があるのだろうと私は疑問に思っていましたcreateRegistry()私はモックを使いたいと知っていますか? (私はしようとしたが、間違いがある)。それは奇妙です、それは実際のオブジェクトとして最初に作成する必要があるオブジェクトをモックにMockitoのようなフレームワークを使用するようなものです...

答えて

1

なぜ.bean(getMockEndpoint("mock:messageParserProcessor"));を持っていますか?私が持っている

getMockEndpoint("mock:MyMockEndpoint").expectedMessageCount(size); 
+0

context.start()後、私はのような何かを主張するmockendpointを使用して、私の@Test方法に続いて

@Before public void setUp() throws Exception { super.setUp(); context.getRouteDefinition("MyRoute").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveById("MyEndPointId").replace().to("mock:MyMockEndpoint"); } }); 

:それは.to("mock:messageParserProcessor")

私は通常、このようなmockendpointsを作成すべきではありません'.bean(...)'というのは、私のルートがどのようなものなのか、 'from'の後の一連のbeanです。とにかく '.to'を試してみましたが、同じことをしました。私は' weaveById'をまだ試していませんでした。私は 'interceptSendToEndpoint'がそのトリックをやると思っていました。 –

+0

私は 'weaveById'でそれを動作させることができました。アドバイスをいただきありがとうございます。 –

+0

クール、嬉しい助けました。 –

関連する問題