JMS与Arquillian

时间:2016-11-30 17:15:49

标签: java jms jboss-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")

1 个答案:

答案 0 :(得分:0)

虽然我的大多数代码都是使一切功能正常化的良好踏脚石,但我缺少两个触发嵌入式测试的关键组件,正确的注释以启动测试以及等待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;