title: spring camel test using multiple contexts
body: <p>I have two camel contexts (A and B) at different xml using Spring. When I load just one context my junit works, but when a I try to load both contexts the Endpoint inject fail running junit.</p>
  
  <p>So, someone have a sample how to use Test using multiple context with spring camel?</p>
  
  <p>Spring Test</p>
  
  <pre class="lang-java prettyprint-override"><code>public class BaseSpringTest extends CamelSpringTestSupport
  {
      protected AbstractXmlApplicationContext createApplicationContext()
      {
          return new ClassPathXmlApplicationContext("camel-config.xml");
      }
  } 
  </code></pre>
  
  <p>My file <code>camel-config.xml</code></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;beans&gt;
   &lt;context:annotation-config/&gt;
   &lt;import resource="classpath:camel-test-dao.xml" /&gt;
   &lt;import resource="classpath:camel-contextA.xml"/&gt;
   &lt;import resource="classpath:camel-contextB.xml"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>My contexts:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;camelContext xmlns="camel.apache.org/schema/spring" id="contextA"&gt; 
   ...
  &lt;/camelContext&gt;
  &lt;camelContext xmlns="camel.apache.org/schema/spring" id="contextB"&gt; 
  ...
  &lt;/camelContext&gt;
  </code></pre>
  
  <p>My unit test, failing at inject <code>Endpoint</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@EndpointInject(uri = "direct:myroute", context="contextB")
  private Endpoint eFooTest;
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre><code>org.apache.camel.spring.GenericBeansException: Error post processing bean: com.mycompany.test.FooTest; nested exception is java.lang.NullPointerException
    at org.apache.camel.spring.CamelBeanPostProcessor.postProcessBeforeInitialization(CamelBeanPostProcessor.java:154)
    at org.apache.camel.test.spring.CamelSpringTestSupport.postProcessTest(CamelSpringTestSupport.java:62)
    at org.apache.camel.test.junit4.CamelTestSupport.doSetUp(CamelTestSupport.java:319)
    at org.apache.camel.test.junit4.CamelTestSupport.setUp(CamelTestSupport.java:238)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    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:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    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)
  Caused by: java.lang.NullPointerException
    at org.apache.camel.impl.CamelPostProcessorHelper.matchContext(CamelPostProcessorHelper.java:84)
    at org.apache.camel.impl.DefaultCamelBeanPostProcessor$1.doWith(DefaultCamelBeanPostProcessor.java:181)
    at org.apache.camel.util.ReflectionHelper.doWithFields(ReflectionHelper.java:73)
    at org.apache.camel.impl.DefaultCamelBeanPostProcessor.injectFields(DefaultCamelBeanPostProcessor.java:168)
    at org.apache.camel.impl.DefaultCamelBeanPostProcessor.postProcessBeforeInitialization(DefaultCamelBeanPostProcessor.java:82)
    at org.apache.camel.spring.CamelBeanPostProcessor.postProcessBeforeInitialization(CamelBeanPostProcessor.java:148)
    ... 31 more
  </code></pre>
  
  <p>Apparently there is a bug at CamelBeanPostProcessor, when there are more one context a null value is returned! </p>
  
  <blockquote>
    <p>if (contexts != null &amp;&amp; contexts.size() == 1) {</p>
  </blockquote>
  
  <pre class="lang-java prettyprint-override"><code>@XmlTransient
  private final DefaultCamelBeanPostProcessor delegate = new DefaultCamelBeanPostProcessor() {
      @Override
      public CamelContext getOrLookupCamelContext() {
          if (camelContext == null) {
              if (camelId != null) {
                  LOG.trace("Looking up CamelContext by id: {} from Spring ApplicationContext: {}", camelId, applicationContext);
                  camelContext = applicationContext.getBean(camelId, CamelContext.class);
              } else {
                  // lookup by type and grab the single CamelContext if exists
                  LOG.trace("Looking up CamelContext by type from Spring ApplicationContext: {}", applicationContext);
                  Map&lt;String, CamelContext&gt; contexts = applicationContext.getBeansOfType(CamelContext.class);
                  if (contexts != null &amp;&amp; contexts.size() == 1) {
                      camelContext = contexts.values().iterator().next();
                  }
              }
          }
          return camelContext;
      }
  </code></pre>
  
  <p><code>Camel 2.16.2</code>
  <code>Spring 4.1.5</code>
  <code>JDK 1.7</code>
  <code>JDK 1.8</code></p>