Copy to ClipboardCopied!Toggle word wrapToggle overflow
그런 다음 Camel의 adviceWith 기능을 사용하여 다음과 같이 단위 테스트에서 지정된 경로에 있는 모든 엔드포인트를 모를 수 있습니다.
모든 엔드 포인트를 조작할 때 advicewith mocking
@Test
public void testAdvisedMockEndpoints() throws Exception {
// advice the start route using the inlined AdviceWith lambda style route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context, "start", a ->
// mock all endpoints
a.mockEndpoints());
getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// additional test to ensure correct endpoints in registry
assertNotNull(context.hasEndpoint("direct:start"));
assertNotNull(context.hasEndpoint("direct:foo"));
assertNotNull(context.hasEndpoint("log:foo"));
assertNotNull(context.hasEndpoint("mock:result"));
// all the endpoints was mocked
assertNotNull(context.hasEndpoint("mock:direct:start"));
assertNotNull(context.hasEndpoint("mock:direct:foo"));
assertNotNull(context.hasEndpoint("mock:log:foo"));
}
@Test
public void testAdvisedMockEndpoints() throws Exception {
// advice the start route using the inlined AdviceWith lambda style route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context, "start", a ->
// mock all endpoints
a.mockEndpoints());
getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// additional test to ensure correct endpoints in registry
assertNotNull(context.hasEndpoint("direct:start"));
assertNotNull(context.hasEndpoint("direct:foo"));
assertNotNull(context.hasEndpoint("log:foo"));
assertNotNull(context.hasEndpoint("mock:result"));
// all the endpoints was mocked
assertNotNull(context.hasEndpoint("mock:direct:start"));
assertNotNull(context.hasEndpoint("mock:direct:foo"));
assertNotNull(context.hasEndpoint("mock:log:foo"));
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
mock 끝점에는 URI mock:<endpoint >가 지정됩니다(예: mock:direct:foo ). INFO 의 Camel 로그는 변조되는 끝점의 수준입니다.
INFO Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]
INFO Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]
Copy to ClipboardCopied!Toggle word wrapToggle overflow
참고
모의 끝점에는 매개 변수가 없는 엔드 포인트의 매개 변수가 제거됩니다. 예를 들어 끝점 log:foo?showAll=true 는 다음 끝점 mock:log:foo 로 호출됩니다. 매개 변수가 제거되었습니다.
또한 패턴을 사용하여 특정 끝점을 변조할 수도 있습니다. 예를 들어 다음과 같이 수행하는 모든 로그 끝점을 추출하려면 다음을 수행합니다.
adviceWith mocking 패턴을 사용하여 끝점만 기록
@Test
public void testAdvisedMockEndpointsWithPattern() throws Exception {
// advice the start route using the inlined AdviceWith lambda style route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context, "start", a ->
// mock only log endpoints
a.mockEndpoints("log*"));
// now we can refer to log:foo as a mock and set our expectations
getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// additional test to ensure correct endpoints in registry
assertNotNull(context.hasEndpoint("direct:start"));
assertNotNull(context.hasEndpoint("direct:foo"));
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"));
}
@Test
public void testAdvisedMockEndpointsWithPattern() throws Exception {
// advice the start route using the inlined AdviceWith lambda style route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context, "start", a ->
// mock only log endpoints
a.mockEndpoints("log*"));
// now we can refer to log:foo as a mock and set our expectations
getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// additional test to ensure correct endpoints in registry
assertNotNull(context.hasEndpoint("direct:start"));
assertNotNull(context.hasEndpoint("direct:foo"));
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"));
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
지원되는 패턴은 와일드카드 또는 정규식일 수 있습니다. Camel에서 사용하는 것과 동일한 일치 기능으로 Intercept에 대한 자세한 내용은 Intercept에서 참조하십시오.
참고
mocking 끝점은 mock에 도달하면 메시지가 복사되도록 합니다. 따라서 Camel이 더 많은 메모리를 사용하게 됩니다. 이 방법은 많은 메시지를 보낼 때 적합하지 않을 수 있습니다.