bij*_*anu 6 java apache-camel spring-boot spring-boot-test spring-camel
我有spring boot app,有1.5.8版本的spring boot。 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);
}
}
Run Code Online (Sandbox Code Playgroud)
Und简单测试:
//@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();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,它运行正常,并且收到一条消息,并且端点满意。如果添加@SpringBootTest注释,则测试失败?为什么呢 当我运行maven clean install时也没有注释,它也会失败:(
任何人都知道此注释对骆驼测试有何作用,或者我如何对其进行调整以使其起作用?
谢谢
这是最后的工作:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
尝试添加注释@RunWith(CamelSpringBootRunner.class)。根据文档:
实现将功能引入
CamelSpringTestSupport基于Spring Boot Test的测试用例的实现。这种方法使开发人员可以使用典型的Spring Test约定对测试开发进行基于Spring Boot的应用程序/路由的测试。
另外,考虑添加@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)和@DisableJmx(true)。第一个在每种测试方法之后清除Spring Context,这将避免在同一测试用例中其他测试留下的任何后果(例如,显然没有理由失败的测试)。
后面的将禁用JMX,因为您在测试中不需要它。
在官方的文档有大约有春节启动运行Apache骆驼的更多信息。这里有一个摘录的例子:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
希望能有所帮助。
| 归档时间: |
|
| 查看次数: |
6803 次 |
| 最近记录: |