1.3.9. 测试 JUnit 5 方法
要测试,Maven 用户需要在 pom.xml 中添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.1</version> <!-- Use the same version as your Spring Boot version -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring-junit5</artifactId>
<version>3.14.2.redhat-00067</version> <!-- use the same version as your Camel core version -->
<scope>test</scope>
</dependency>
要测试 Camel Spring Boot 应用程序,请使用 @CamelSpringBootTest 给您的测试类标注。这会将 Camel 的 Spring Test 支持引入到应用程序中,以便您可以使用 Spring Boot 测试规则编写测试。
要获取 CamelContext 或 ProducerTemplate,您可以使用 @Autowired 来将它们注入到类。
您还可以使用 camel-test-spring-junit5 配置声明性测试。本例使用 @MockEndpoints 注解来自动模拟端点:
@CamelSpringBootTest
@SpringBootApplication
@MockEndpoints("direct:end")
public class MyApplicationTest {
@Autowired
private ProducerTemplate template;
@EndpointInject("mock:direct:end")
private MockEndpoint mock;
@Test
public void testReceive() throws Exception {
mock.expectedBodiesReceived("Hello");
template.sendBody("direct:start", "Hello");
mock.assertIsSatisfied();
}
}