我有春季启动应用程序,弹簧启动版本为1.5.8,camel 2.20.1
简单路线:
@Component
public class MyRoute extends RouteBuilder {
public static final String IN = "file://in";
public static final String OUT = "file://out";
@Override
public void configure() throws Exception {
from(IN).routeId("myId").to(OUT);
}
}
简单测试:
//@SpringBootTest
public class MyRouteTest extends CamelTestSupport {
@Produce(uri = MyRoute.IN)
private ProducerTemplate producerTemplate;
@EndpointInject(uri = "mock:file:out")
private MockEndpoint mockEndpointOut;
@Override
public String isMockEndpoints() {
return "*";
}
@Test
public void simpleTest() throws Exception {
mockEndpointOut.expectedMessageCount(1);
producerTemplate.sendBody("Test");
mockEndpointOut.assertIsSatisfied();
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new MyRoute();
}
}
当我运行此测试时,它运行正常,我得到一条消息,并且端点满意。
如果我添加@SpringBootTest
注释,测试会失败吗?为什么?
当我运行maven clean install时也没有注释它也失败了:(
任何人都知道这个注释对骆驼测试有什么作用,或者我如何调整它以使其有效?
THX
答案 0 :(得分:5)
最后有效:
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
public class MyRouteTest2 {
@Autowired
private ProducerTemplate producerTemplate;
@EndpointInject(uri = "mock:file:out")
private MockEndpoint mockCamel;
@Test
public void test() throws InterruptedException {
String body = "Camel";
mockCamel.expectedMessageCount(1);
producerTemplate.sendBody("file:in", body);
mockCamel.assertIsSatisfied();
}
}
答案 1 :(得分:2)
尝试添加注释@RunWith(CamelSpringBootRunner.class)
。根据{{3}}:
将
CamelSpringTestSupport
的功能引入基于Spring Boot Test的测试用例的实现。这种方法允许开发人员使用典型的测试开发Spring测试约定来实现基于Spring Boot的应用程序/路由的测试。
另外,请考虑添加@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
和@DisableJmx(true)
。第一个在每个测试方法之后清除Spring上下文,这将避免在同一测试用例中从其他测试中留下任何后果(比如没有理由的测试失败)。
后者将禁用JMX,因为您的测试不需要它。
docs提供了有关使用Spring Boot运行Apache Camel的更多信息。这里有一个示例摘录:
@ActiveProfiles("test")
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(true)
public class MyRouteTest extends CamelTestSupport {
@Autowired
private CamelContext camelContext;
@Override
protected CamelContext createCamelContext() throws Exception {
return camelContext;
}
@EndpointInject(uri = "direct:myEndpoint")
private ProducerTemplate endpoint;
@Override
public void setUp() throws Exception {
super.setUp();
RouteDefinition definition = context().getRouteDefinitions().get(0);
definition.adviceWith(context(), new RouteBuilder() {
@Override
public void configure() throws Exception {
onException(Exception.class).maximumRedeliveries(0);
}
});
}
@Override
public String isMockEndpointsAndSkip() {
return "myEndpoint:put*";
}
@Test
public void shouldSucceed() throws Exception {
assertNotNull(camelContext);
assertNotNull(endpoint);
String expectedValue = "expectedValue";
MockEndpoint mock = getMockEndpoint("mock:myEndpoint:put");
mock.expectedMessageCount(1);
mock.allMessages().body().isEqualTo(expectedValue);
mock.allMessages().header(MY_HEADER).isEqualTo("testHeader");
endpoint.sendBodyAndHeader("test", MY_HEADER, "testHeader");
mock.assertIsSatisfied();
}
}
希望有所帮助。