activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bruce Snyder" <bruce.sny...@gmail.com>
Subject Re: Question about unit test with activemq
Date Mon, 28 May 2007 13:19:25 GMT
On 5/28/07, Carfield Yim <carfield@carfield.com.hk> wrote:
>
>
>
> Carfield Yim wrote:
> >
> >> How about creating a consumer to receive the message and asserting the
> >> message? There are examples of this throughout the ActiveMQ tests.
> >> There's a really simple one in the
> >> JMSConsumerTest.testReceiveMessageWithConsumer() test:
> >>
> >> http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/JMSConsumerTest.java?view=markup
> >>
> > Can I not do that? Look like pretty complicate for just assert value
> > in unit test.
> >
> >
>
> The reason I ask that because now I setup the test using very simple code:
>
>
> public class JmsQueueSenderTest
> {
>         JmsTemplate102 jt;
>         ActiveMQQueue queue;
>
>         @Before
>         public void setup()
>         {
>                 ConnectionFactory connectionFactory = new
> ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
>                 jt = new JmsTemplate102(connectionFactory, false);
>                 queue = new ActiveMQQueue("queue");
>
>         }
>
>         @Test
>         public void testSimpleSend()
>         {
>                 jt.send(queue, new MessageCreator()
>                 {
>                         public Message createMessage(Session session) throws JMSException
>                         {
>                                 return session.createTextMessage("test");
>                         }
>                 });
>         }
> }
>
> I haven't really create a session and I hope I don't need to do so for just
> a unit test

Well you wanted to send a message through the broker and assert it.
This requires that you start up the broker, create a session, create a
connection, send the message and assert it. That's why I suggested
that you look at the testReceiveMessageWithConsumer() method as one
example to follow.  Below is this method:

    public void testReceiveMessageWithConsumer() throws Exception {

        // Receive a message with the JMS API
        connection.start();
        Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
        destination = createDestination(session, destinationType);
        MessageConsumer consumer = session.createConsumer(destination);

        // Send the messages
        sendMessages(session, destination, 1);

        // Make sure only 1 message was delivered.
        Message m = consumer.receive(1000);
        assertNotNull(m);
        assertEquals("0", ((TextMessage)m).getText());
        assertNull(consumer.receiveNoWait());
    }

Just replace the connection.start() call with the following:

BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("
path/to/activemq.xml"));
        bfb.afterPropertiesSet();
        broker = bfb.getBroker();
        broker.start();

        connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:61216");

And the sendMessages() method is below:

protected void sendMessages(Session session, Destination destination,
int count) throws JMSException {
        MessageProducer producer = session.createProducer(destination);
        for (int i = 0; i < count; i++) {
            producer.send(session.createTextMessage(""+i));
        }
        producer.close();
    }

Bruce
-- 
perl -e 'print unpack("u30","D0G)U8V4\@4VYY9&5R\"F)R=6-E+G-N>61E<D\!G;6%I;\"YC;VT*"
);'

Apache Geronimo - http://geronimo.apache.org/
Apache ActiveMQ - http://activemq.org/
Apache ServiceMix - http://servicemix.org/
Castor - http://castor.org/

Mime
View raw message