title: Apache camel Junit keep Context running
body: <p>We have some JUnits for testing camel routes and business logic, at some point there is a need to have camel context running, until response response received from server for one of the JUnit tests. We are using CamelSpringTestSupport. The problem is, no other JUnits running after that.</p>
  
  <p>MyTest.java is below</p>
  
  <pre><code>import java.io.File;
  
  import org.apache.activemq.camel.component.ActiveMQComponent;
  import org.apache.camel.Exchange;
  import org.apache.camel.builder.RouteBuilder;
  import org.apache.camel.main.Main;
  import org.apache.camel.test.spring.CamelSpringTestSupport;
  import org.junit.Ignore;
  import org.junit.Test;
  import org.springframework.context.support.AbstractXmlApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import org.springframework.stereotype.Component;
  
  @Component
  public class MyTest extends CamelSpringTestSupport{
      String prop1;   
      Main main;
      ActiveMQComponent jms;
  
      public void setUp() throws Exception {
          super.setUp();
          prop1 = context.resolvePropertyPlaceholders("{{prop1}}");        
          jms = (ActiveMQComponent) context.getComponent("jms");
  
          RouteBuilder builder = new RouteBuilder() {
  
              @Override
              public void configure() throws Exception {
                  from("fromUri")
                  .log("Got a message")               
                  .to("file://C:\\Temp\\tempfolder");         
              }
          };        
          main = new Main();               
          main.addRouteBuilder(builder);
          main.bind("jms", jms);
          // add event listener
          //main.addMainListener(new Events());
          // set the properties from a file
          //main.setPropertyPlaceholderLocations("example.properties");
          // run until you terminate the JVM
          System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
          main.run();
      }
  
      @Override
      protected AbstractXmlApplicationContext createApplicationContext() {
          return new ClassPathXmlApplicationContext("/yourcontext.xml");
      }
  
      @Test
      public void testMoveFile() throws Exception {
          // create a new file in the inbox folder with the name hello.txt and containing Hello World as body
          template.sendBodyAndHeader("file://C:\\targetIn", "Hello World", Exchange.FILE_NAME, "hello.txt");
  
          // wait a while to let the file be moved
          Thread.sleep(2000);
  
          // test the file was moved
          File target = new File("C:\\targetin\\hello.txt");
          assertTrue("File should have been moved", target.exists());
  
          // test that its content is correct as well
          String content = context.getTypeConverter().convertTo(String.class, target);
          assertEquals("Hello World", content);
      }
  
      @Test
      public void uploadData() throws Exception{  
          File target = new File("c:\\input.xml");
          assertTrue("File should have been moved", target.exists());
  
          template.sendBody(toUri, target);
      }
  }
  </code></pre>
  
  <p>yourcontext.xml looks like below, i may be doing a basic mistake by having two context?</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:util="http://www.springframework.org/schema/util"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:camel="http://camel.apache.org/schema/spring"
         xmlns:broker="http://activemq.apache.org/schema/core"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
           http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"&gt;
  
      &lt;context:component-scan base-package="com.yourpackage" /&gt;
      &lt;bean id="properties" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer"&gt;
            &lt;property name="ignoreResourceNotFound" value="true"/&gt;
            &lt;property name="locations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath:yourproperties.properties&lt;/value&gt;                              
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;camel:camelContext id="mytest" useMDCLogging="true" threadNamePattern="#camelId#:#name#-##counter#"&gt;
          &lt;camel:contextScan /&gt;
          &lt;camel:jmxAgent id="agent" createConnector="false" /&gt;
          &lt;camel:template id="camelTemplate" /&gt;
      &lt;/camel:camelContext&gt;
  
      &lt;bean id="shutdown" class="org.apache.camel.impl.DefaultShutdownStrategy"&gt;
          &lt;property name="timeout" value="120" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"&gt;
          &lt;property name="connectionFactory"&gt;
              &lt;bean class="org.apache.activemq.spring.ActiveMQConnectionFactory"&gt;
                  &lt;property name="brokerURL" value="tcp://${activemq.host}:${activemq.port}" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>