JMS与Arquillian的集成测试

Integration Test of JMS with Arquillian

我正在尝试测试一个主题是否可以填充到我的容器中。但是,在我的工厂中调用 createConnection 方法时,我不断收到空指针异常。以下是我的代码的执行方式:

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

}

我使用 Arquillian 进行了这样的部署:

@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;
}

我的 Jboss EAP 6.0.0 G2 实现包含以下代码行:

<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>

我这辈子都想不通为什么

final Connection connection = factory.createConnection();

正在抛出 NullPinter。显然,工厂无法实例化,这让我相信 Arquillian 无法查看我的 jndi 绑定。但是,即使尝试这些加载工厂资源的组合也会引发相同的错误:

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

虽然我的大部分代码是使所有功能正常运行的良好垫脚石,但我缺少触发嵌入式测试的两个关键组件、启动测试的正确注释以及等待 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
}

测试用例中的其他所有内容均已正确设置,包括原始资源调用。

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

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