JBoss Community Archive (Read Only)

Arquillian Old

Testing JMS

Here's another JUnit Arquillian test that exercises with JMS, something that may have previously seemed very tricky to test. The test uses a utility class QueueRequestor to encapsulate the low-level code for sending and receiving a message using a queue.

import javax.annotation.Resource;
import javax.jms.*;
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.acme.ejb.MessageEcho;
import com.acme.util.jms.QueueRequestor;

public class InjectionTestCase {
   public static JavaArchive createTestArchive() {
      return ShrinkWrap.create(JavaArchive.class, "test.jar")
         .addClasses(MessageEcho.class, QueueRequestor.class);

   @Resource(mappedName = "/queue/DLQ")
   private Queue dlq;

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

   public void shouldBeAbleToSendMessage() throws Exception {

      String messageBody = "ping";

      Connection connection = factory.createConnection();
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      QueueRequestor requestor = new QueueRequestor((QueueSession) session, dlq);


      Message request = session.createTextMessage(messageBody);
      Message response = requestor.request(request, 5000);

      Assert.assertEquals("Should have responded with same message", messageBody, ((TextMessage) response).getText());
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:18:46 UTC, last content change 2011-04-16 15:27:56 UTC.