2016-11-30 107 views
0

我試圖測試一個主題可以填充到我的容器中。但是,在我的工廠中調用createConnection方法時,我總是收到空指針異常。下面是如何被執行我的代碼:JMS與Arquillian的集成測試

@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") 

回答

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;