简体   繁体   English

JMS与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. 但是,在我的工厂中调用createConnection方法时,我总是收到空指针异常。 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: 我在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;
}

My Jboss EAP 6.0.0 G2 implementation contains the following lines of code: 我的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>

I cannot for the life of me figure out why 我无法为自己的生活弄清楚为什么

final Connection connection = factory.createConnection();

is throwing a NullPinter. 抛出一个NullPinter。 Obviously, the factory is unable to get instantiated, leading me to believe Arquillian is unable to look at my jndi bindings. 显然,工厂无法实例化,使我相信Arquillian无法查看我的jndi绑定。 However, even trying these combinations of loading the factory resource throws the same error: 但是,即使尝试这些加载工厂资源的组合也会引发相同的错误:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM