簡體   English   中英

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM