简体   繁体   English

Camel JUnit中的Autowire Spring Bean

[英]Autowire Spring Bean in Camel JUnit

I'm trying to integrate Camel with Spring 5 (using annotations - no XML!) and am having some trouble with my JUnit tests. 我正在尝试将Camel与Spring 5集成(使用注释-不使用XML!),并且在JUnit测试中遇到了一些麻烦。 I am able to launch Camel when the JUnit runs, but the Spring controller object I am trying to invoke as part of the test is not getting autowired. 当JUnit运行时,我能够启动Camel,但是作为测试的一部分,我试图调用的Spring控制器对象没有自动接线。

My Junit is below; 我的Junit在下面;

public class ListCaseNotesTest extends CamelSpringTestSupport {

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext();
        acc.register(CamelController.class);
        return acc;
    }

    @Autowired
    VisitsController visitsController;

    @Test
    public void testListCaseNotes() throws Exception{

        ListNotesKey listNotesKey = new ListNotesKey();
        listNotesKey.setUsername("caseworker");
        listNotesKey.setRelatedIDs("[\"1000\",\"1001\",\"1002\"]");
        listNotesKey.setRelatedType(ListNotesKey.RelatedTypeEnum.CASE);

        // invoke camel
        String response = visitsController.sendMessage("listCaseNotes", SOR_CONFIG, payload);

I've ommitted the response validation for brevity - the exception I receive is a NullPointerException when the sendMessage is invoked. 为了简洁起见,我省略了响应验证-调用sendMessage时收到的异常是NullPointerException。 The full log is below; 完整日志如下:

Mar 01, 2018 3:53:04 PM org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'visitsRouteBuilder' of type [com.me.camel.routes.VisitsRouteBuilder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Mar 01, 2018 3:53:04 PM org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'camelController' of type [com.me.camel.CamelController$$EnhancerBySpringCGLIB$$4c13afa5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[                          main] SpringCamelContext             INFO  Apache Camel 2.20.2 (CamelContext: camel-1) is starting
[                          main] ManagedManagementStrategy      INFO  JMX is enabled
[                          main] DefaultTypeConverter           INFO  Type converters loaded (core: 192, classpath: 1)
[                          main] SpringCamelContext             INFO  StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
[                          main] SpringCamelContext             INFO  Route: route1 started and consuming from: direct://visits
[                          main] SpringCamelContext             INFO  Total 1 routes, of which 1 are started
[                          main] SpringCamelContext             INFO  Apache Camel 2.20.2 (CamelContext: camel-1) started in 0.443 seconds
[                          main] ListCaseNotesTest              INFO  ********************************************************************************
[                          main] ListCaseNotesTest              INFO  Testing done: testListCaseNotes(com.me.test.notes.ListCaseNotesTest)
[                          main] ListCaseNotesTest              INFO  Took: 0.034 seconds (34 millis)
[                          main] ListCaseNotesTest              INFO  ********************************************************************************
[                          main] SpringCamelContext             INFO  Apache Camel 2.20.2 (CamelContext: camel-1) is shutting down
[                          main] DefaultShutdownStrategy        INFO  Starting to graceful shutdown 1 routes (timeout 10 seconds)
[el-1) thread #1 - ShutdownTask] DefaultShutdownStrategy        INFO  Route: route1 shutdown complete, was consuming from: direct://visits
[                          main] DefaultShutdownStrategy        INFO  Graceful shutdown of 1 routes completed in 0 seconds
Mar 01, 2018 3:53:05 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@e056f20: startup date [Thu Mar 01 15:53:04 EST 2018]; root of context hierarchy
Mar 01, 2018 3:53:05 PM org.springframework.context.support.DefaultLifecycleProcessor stop
INFO: Stopping beans in phase 2147483647
[                          main] SpringCamelContext             INFO  Apache Camel 2.20.2 (CamelContext: camel-1) uptime 0.496 seconds
[                          main] SpringCamelContext             INFO  Apache Camel 2.20.2 (CamelContext: camel-1) is shutdown in 0.017 seconds

java.lang.NullPointerException
    at com.me.test.notes.ListCaseNotesTest.testListCaseNotes(ListCaseNotesTest.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code -1

What do I need to do to set up my JUnit test such that the Spring bean is autowired successfully? 我需要做些什么来设置我的JUnit测试,以便成功自动装配Spring bean?

Thanks in advance! 提前致谢!

Bic 比克

According to https://camel.apache.org/spring-testing.html the CamelSpringTestSupport doesn't process the Spring annotations. 根据https://camel.apache.org/spring-testing.html的描述, CamelSpringTestSupport不处理Spring注释。 If you insist on extending it you could get your context from applicationContext field and manually get the right bean: 如果您坚持要扩展它,则可以从applicationContext字段获取上下文并手动获取正确的bean:

applicationContext.getBean(VisitsController.class).sendMessage("listCaseNotes", SOR_CONFIG, payload)

Alternatively switch to Cammel runner @RunWith(org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunner) . 或者切换到CammelRunner @RunWith(org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunner)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM