title: How to mock connection factory in spring integration
body: <p>I would like to mock the JMS connection factory to do integration testing. </p>
  
  <pre><code>&lt;jee:jndi-lookup id="narconFactory" jndi-name="LBARQCF" resource-ref="false"/&gt;
  </code></pre>
  
  <p>when (?)</p>
  
  <p>we use websphere server, and for intyegration testing need to use tomcat. Spring boot version is 1.5.3 </p>
  
  <p><strong>EDIT</strong>:
  Based on comments, I created a class</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  public class MQControllerTest {
  
  //step 2
  @Autowired
  private MockMvc mvc;
  
  @MockBean
  private MQController mqController;
  
  protected static final ConnectionFactory amqFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
  
  public static final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(amqFactory);
  
  @BeforeClass
  public static void startUp() throws Exception {
      connectionFactory.setCacheConsumers(false);
      connectionFactory.createConnection().close();
  }
  
  @AfterClass
  public static void shutDown() {
      connectionFactory.resetConnection();
  }
  
  @Autowired
  private JmsTemplate mockJmsTemplate;
  
  @Autowired
  private SourcePollingChannelAdapter startJms;
  
  
  @Before
  public void setup() throws JMSException{
      Mockito.reset(this.mockJmsTemplate);
  }   
  
  @Test
  public void test_tbm_vrijeDagenCorrectie() throws Exception {
  
      String expectedResponse ="";
      //using bddmockito
      given(mqController.tmVDCorrect(any(String.class))).willReturn(expectedResponse);
  
      this.startJms.start();
  
      mvc.perform(get("/tm/vdc/{dgn}","2016-01-16"))
                 .andExpect(status().isOk());
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In my resources - config file ** edit*** used full package name and made connectionFactory as public</p>
  
  <pre><code>    &lt;util:constant id="narconFactory" static-field="n.d.poc.MQControllerTest.connectionFactory"/&gt;
       &lt;util:constant id="vanconFactory" static-field="n.d.poc.MQControllerTest.connectionFactory"/&gt; 
  </code></pre>
  
  <p><strike>
  Error:</p>
  
  <blockquote>
    <p>No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]</p>
  </blockquote>
  
  <p>Not sure whats my mistake
  </strike>
  <strong>EDIT2:</strong></p>
  
  <blockquote>
    <p>Caused by: org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'narconFactory': Invocation of
    init method failed; nested exception is
    javax.naming.NoInitialContextException: Need to specify class name in
    environment or system property, or as an applet parameter, or in an
    application resource file:  java.naming.factory.initial   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742)
    ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]   at
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]     at
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]     at
    org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
    ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]    at
    org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
    ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]    at
    org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
    ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]    at
    org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
    ~[spring-boot-test-1.5.3.RELEASE.jar:1.5.3.RELEASE]   at
    org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    ~[spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at
    org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ~[spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]    ... 27 common frames
    omitted</p>
  </blockquote>
  
  <p>why unit testing is looking for java.naming.factory.initial? can I provide in application properties and with what value?</p>
  
  <p><strong>EDIT3:</strong></p>
  
  <blockquote>
    <p>sample code in github 
    <a href="https://github.com/kswat/LRIntegrationUnit" rel="nofollow noreferrer">https://github.com/kswat/LRIntegrationUnit</a></p>
  </blockquote>