44.12. エンドポイントをモックし、元のエンドポイントへの送信をスキップする
特定のエンドポイントへの送信を簡単にモックしてスキップしたい場合があります。したがって、メッセージは迂回され、モックエンドポイントのみに送信されます。AdviceWith を使用して、mockEndpointsAndSkip メソッドを使用できるようになりました。以下の例では、2 つのエンドポイント "direct:foo" と "direct:bar" への送信をスキップします。
エンドポイントへの送信をモックしてスキップするアドバイス
@Test
public void testAdvisedMockEndpointsWithSkip() throws Exception {
// advice the first route using the inlined AdviceWith route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context.getRouteDefinitions().get(0), context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// mock sending to direct:foo and direct:bar and skip send to it
mockEndpointsAndSkip("direct:foo", "direct:bar");
}
});
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
getMockEndpoint("mock:direct:bar").expectedMessageCount(1);
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// the message was not send to the direct:foo route and thus not sent to
// the seda endpoint
SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
assertEquals(0, seda.getCurrentQueueSize());
}
@Test
public void testAdvisedMockEndpointsWithSkip() throws Exception {
// advice the first route using the inlined AdviceWith route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context.getRouteDefinitions().get(0), context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// mock sending to direct:foo and direct:bar and skip send to it
mockEndpointsAndSkip("direct:foo", "direct:bar");
}
});
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
getMockEndpoint("mock:direct:bar").expectedMessageCount(1);
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// the message was not send to the direct:foo route and thus not sent to
// the seda endpoint
SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
assertEquals(0, seda.getCurrentQueueSize());
}
テストキットを使用した同じ例
isMockEndpointsAndSkip using camel-test kit
public class IsMockEndpointsAndSkipJUnit4Test extends CamelTestSupport {
@Override
public String isMockEndpointsAndSkip() {
// override this method and return the pattern for which endpoints to mock,
// and skip sending to the original endpoint.
return "direct:foo";
}
@Test
public void testMockEndpointAndSkip() throws Exception {
// notice we have automatic mocked the direct:foo endpoints and the name of the endpoints is "mock:uri"
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// the message was not send to the direct:foo route and thus not sent to the seda endpoint
SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
assertEquals(0, seda.getCurrentQueueSize());
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("direct:foo").to("mock:result");
from("direct:foo").transform(constant("Bye World")).to("seda:foo");
}
};
}
}
public class IsMockEndpointsAndSkipJUnit4Test extends CamelTestSupport {
@Override
public String isMockEndpointsAndSkip() {
// override this method and return the pattern for which endpoints to mock,
// and skip sending to the original endpoint.
return "direct:foo";
}
@Test
public void testMockEndpointAndSkip() throws Exception {
// notice we have automatic mocked the direct:foo endpoints and the name of the endpoints is "mock:uri"
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// the message was not send to the direct:foo route and thus not sent to the seda endpoint
SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
assertEquals(0, seda.getCurrentQueueSize());
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("direct:foo").to("mock:result");
from("direct:foo").transform(constant("Bye World")).to("seda:foo");
}
};
}
}