1.2.11. 测试 JUnit 5 方法
要进行测试,Maven 用户需要将以下依赖项添加到其 pom.xml 中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.6</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.18.3.redhat-00022</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();
}
}