1
votes

I would like to mock the JMS connection factory to do integration testing.

<jee:jndi-lookup id="narconFactory" jndi-name="LBARQCF" resource-ref="false"/>

when (?)

we use websphere server, and for intyegration testing need to use tomcat. Spring boot version is 1.5.3

EDIT: Based on comments, I created a class

@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());

}

}

In my resources - config file ** edit*** used full package name and made connectionFactory as public

    <util:constant id="narconFactory" static-field="n.d.poc.MQControllerTest.connectionFactory"/>
     <util:constant id="vanconFactory" static-field="n.d.poc.MQControllerTest.connectionFactory"/> 

Error:

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]

Not sure whats my mistake EDIT2:

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

why unit testing is looking for java.naming.factory.initial? can I provide in application properties and with what value?

EDIT3:

sample code in github https://github.com/kswat/LRIntegrationUnit

1

1 Answers

1
votes

I would suggest to consider to use ActiveMQ with its embedded mode instead of mocking. This way you'll get the fully blown JMS and won't deal with low-level resources to also mock.

In Spring Integration we have this for testing purpose:

/**
 * Keeps an ActiveMQ {@link VMTransport} open for the duration of
 * all tests (avoids cycling the transport each time the last
 * connection is closed).
 * @author Gary Russell
 * @since 3.0
 *
 */
public abstract class ActiveMQMultiContextTests {

    protected static final ConnectionFactory amqFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    protected 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();
    }

}

That connectionFactory static property indeed can be used as a bean:

<util:constant id="narconFactory"
               static-field="ActiveMQMultiContextTests.connectionFactory"/>