我定义了以下路线:
from(direct(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER))
.routeId(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER)
// Split the usernames on an individual exchange each. This property is a Set<String>.
.split(simpleF("${exchangeProperty.%s}", ExchangeProperty.USERNAMES))
// Set the flag for the next route's processor.
.setProperty(ExchangeProperty.SINGLE_EMAIL_PER_USER, constant(true))
.to(direct(Routes.SEND_EMAIL_BOP))
.end();
并编写了以下测试:
@QuarkusTest
public class EmailRouteBuilderTest extends CamelQuarkusTestSupport {
@Inject
ProducerTemplate producerTemplate;
@Override
public String isMockEndpoints() {
return "*";
}
@Test
void testIndividualEmailPerUser() throws InterruptedException {
final Set<String> usernames = Set.of("a", "b", "c", "d", "e");
final Exchange exchange = this.createExchangeWithBody("");
exchange.setProperty(ExchangeProperty.USERNAMES, usernames);
final MockEndpoint sendEmailBopEndpoint = this.getMockEndpoint(String.format("mock:direct:%s", Routes.SEND_EMAIL_BOP), false);
sendEmailBopEndpoint.expectedMessageCount(5);
this.producerTemplate.send(String.format("direct:%s", Routes.SEND_EMAIL_BOP_SINGLE_PER_USER), exchange);
sendEmailBopEndpoint.assertIsSatisfied();
}
}
但是,当我运行测试时,我看到来自
Routes.SEND_EMAIL_BOP
路由的各种错误消息,该路由尝试联系真实服务,并且断言失败,并显示“预期 5 条消息,收到 0 条消息”。日志显示以下内容:
2023-08-31 09:51:55,896 INFO [org.apa.cam.com.moc.InterceptSendToMockEndpointStrategy] (main) Adviced endpoint [direct://send-email-single-per-user] with mock endpoint [mock:direct:send-email-single-per-user]
2023-08-31 09:51:55,897 INFO [org.apa.cam.com.moc.InterceptSendToMockEndpointStrategy] (main) Adviced endpoint [direct://send-email-bop] with mock endpoint [mock:direct:send-email-bop]
2023-08-31 09:51:55,896 INFO [org.apa.cam.com.moc.InterceptSendToMockEndpointStrategy] (main) Adviced endpoint [http://boplocalhost:9999] with mock endpoint [mock:http:boplocalhost:9999]
日志的最后一行对应的是服务。我认为我对整个事情的理解是错误的:我认为模拟端点使 Camel 无法调用真正的服务或后续路由。
我尝试将
isMockEndpoints
替换为 isMockEndpointsAndSkip
但测试效果不佳。
您可以帮我理解我做错了什么吗?
提前非常感谢您。
模拟端点本质上会导致端点被检测,以便测试可以断言有关端点的事情,例如它收到了多少条消息。但是,被模拟的端点仍然会被执行。模拟并跳过采取额外步骤来防止原始端点被执行。
不清楚您所说的“模拟并跳过”导致您的测试无法正常工作是什么意思。如果您需要模拟返回存根值,请查看MockEndpoint.whenAnyExchangeReceived()。