2.40.3. 使用方法
要在测试中使用 camel-mock 功能,才能访问 MockEndpoint 实例。
CDI 注入可用于访问实例(请参阅 Quarkus 文档)。您可以使用 @Inject
注释将 camelContext 注入到测试中。然后,Camel 上下文可用于获取模拟端点。请参见以下示例:
import javax.inject.Inject; import org.apache.camel.CamelContext; import org.apache.camel.ProducerTemplate; import org.apache.camel.component.mock.MockEndpoint; import org.junit.jupiter.api.Test; import io.quarkus.test.junit.QuarkusTest; @QuarkusTest public class MockJvmTest { @Inject CamelContext camelContext; @Inject ProducerTemplate producerTemplate; @Test public void test() throws InterruptedException { producerTemplate.sendBody("direct:start", "Hello World"); MockEndpoint mockEndpoint = camelContext.getEndpoint("mock:result", MockEndpoint.class); mockEndpoint.expectedBodiesReceived("Hello World"); mockEndpoint.assertIsSatisfied(); } }
用于示例测试的路由:
import javax.enterprise.context.ApplicationScoped; import org.apache.camel.builder.RouteBuilder; @ApplicationScoped public class MockRoute extends RouteBuilder { @Override public void configure() throws Exception { from("direct:start").to("mock:result"); } }