我必须使用 JUnit 测试我的路由构建器,并且我得到了 java.lang.NoSuchMethodError,我是否缺少任何配置或我访问路由构建器的方式应该更改。请在此处找到 RouteBuilder、context 和 Junit 类
@Component
public class MyRouteBuilder extends SpringRouteBuilder {
@Autowired
private SourcePoint sourcePoint;
@Autowired
private DestinationPoint destinationPoint;
@Override
public void configure() throws Exception {
from(sourcePoint.getString()).to(destinationPoint.getString());
}
我创建了 JUnit 测试用例,它的上下文位于适当的 maven 位置,如下所示
src/test/java/org/apache/camel/spring/patterns/MyRouteBuilderTest.java
src/test/resources/org/apache/camel/spring/patterns/MyRouteBuilderTest-context.xml
在这里,我附加了这两个文件
***** MyRouteBuilderTest-context.xml ***
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="MyRouteBuilder" />
</camelContext>
<context:component-scan base-package="com.myroute" />
***** MyRouteBuilderTest.class ***
@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTest.class)
@ContextConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
public class EmypaRouteBuilderTest extends CamelSpringTestSupport {
private static final Logger logger = LoggerFactory.getLogger(MyRouteBuilderTest.class);
@Autowired
private MyRouteBuilder myRouteBuilder = null;
@Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("Application-context.xml");
}
@Test
public void testRead throws exception(){
context.start();
Thread.sleep(10 * 60 * 1000);
}
}
为了您的验证,我也附加了日志
java.lang.NoSuchMethodError: org.springframework.test.context.TestContextManager.<init>(Ljava/lang/Class;Ljava/lang/String;)V
at org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner$CamelTestContextManager.<init>(CamelSpringJUnit4ClassRunner.java:68)
at org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner.createTestContextManager(CamelSpringJUnit4ClassRunner.java:47)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:114)
at org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner.<init>(CamelSpringJUnit4ClassRunner.java:36)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
使用 camel-test-spring 时,您需要将 Spring 4.0.x 与 Camel 2.14.x 一起使用,因为它不支持 Spring 4.1。对 Spring 4.1 的支持将在 Camel 2.15 中推出,现在有 2 个弹簧测试模块
- 骆驼测试弹簧40 - 适用于春季 4.0.x
- 骆驼测试弹簧 - 适用于春季 4.1 及以上
这也记录在发行说明中,在考虑 seciton 的重要更改中
- http://camel.apache.org/camel-2140-release.html