Search code examples
javajmsjboss-arquillian

Integration Test of JMS with Arquillian


I am attempting to test that a topic can be filled in my container. However, I keep getting a null pointer exception when calling the createConnection method within my factory. Here is how my code is being executed:

@RunWith(Arquillian.class)
public class TopicPublishTest {

    @Resource(mappedName = "java:jboss/jms/topic/sample/MySample")
    private Topic topic;

    @Resource(mappedName = "java:/ConnectionFactory")
    private ConnectionFactory factory;

    @Test
    public void testMessageInTopic() throws Exception {
        final Connection connection = factory.createConnection();
        connection.start();
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        final MessageConsumer consumer = session.createConsumer(topic);
        final TextMessage message = (TextMessage) consumer.receiveNoWait();
        System.out.println("### the mssage is " + message);
    }

}

I have a deployment with Arquillian as this:

@Deployment(name = "my-service", order = 1, testable = true)
public static Archive<?> targetDeployment() {
    final WebArchive archive = ShrinkWrap.createFromZipFile(WebArchive.class, new File(
            "target/my-service.war"));
    return archive;
}

My Jboss EAP 6.0.0 G2 implementation contains the following lines of code:

<connection-factory name="InVmConnectionFactory">
    <connectors>
        <connector-ref connector-name="in-vm"/>
    </connectors>
    <entries>
        <entry name="java:/ConnectionFactory"/>
    </entries>
</connection-factory>

....

<jms-topic name="MySample">
    <entry name="java:jboss/jms/topic/sample/MySample"/>
    <entry name="java:jboss/exported/jms/topic/sample/MySample"/>
</jms-topic>

I cannot for the life of me figure out why

final Connection connection = factory.createConnection();

is throwing a NullPinter. Obviously, the factory is unable to get instantiated, leading me to believe Arquillian is unable to look at my jndi bindings. However, even trying these combinations of loading the factory resource throws the same error:

@Resource(mappedName = "/ConnectionFactory")
@Resource(mappedName = "ConnectionFactory")

Solution

  • While the majority of my code was a good stepping stone towards making everything functional, I was missing two key components to trigger the embedded test, the correct annotation to start the test and the correct way to wait for jms.

    @Test
    @OperateOnDeployment("my-service")
    public void testMessageInTopic() throws Exception {
        // insert the message into the topic
        final TextMessage message = (TextMessage) consumer.receive(15000);
        // perform assertions after message received, not null, text, etc
    }
    

    Everything else in the test cases is setup correctly, including the original resource invocations.

    @Resource(mappedName = "java:jboss/jms/topic/sample/MySample")
    private Topic topic;
    
    @Resource(mappedName = "java:/ConnectionFactory")
    private ConnectionFactory factory;