You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Carfield Yim <ca...@carfield.com.hk> on 2007/05/26 14:40:49 UTC

Question about unit test with activemq

I've create a testcase according to
http://activemq.apache.org/how-to-unit-test-jms-code.html . However,
after I send the message, how can I assert my message really sent?

Re: Question about unit test with activemq

Posted by Carfield Yim <ca...@carfield.com.hk>.

bsnyder wrote:
> 
> 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:
> 

The reason that I don't want to do that is I don't really need to create a
session and consumer detail myself, spring will handle it. I would like to
make the test using similar functioniltiy of spring to make thing simple.

Here is my simple util class of doing so, it may not really elegant but it
work for me, see if anyone interested to reuse that


public class TestJmsTemplate extends JmsTemplate102
{
	public final JmsQueueSender sender = new JmsQueueSender();

	public TestJmsTemplate() throws JMSException
	{
		super(new ActiveMQConnectionFactory("vm://localhost"), false);
		final ActiveMQQueue activeMQQueue = new ActiveMQQueue("queue");
		sender.setJt(this);
		sender.setQueue(activeMQQueue);
	}

	public void assertMessage(final String text) throws JMSException
	{
		final Message m = receive(sender.getQueue());
		assertNotNull(m);
		assertEquals(text, ((TextMessage) m).getText());
	}
}

public class JmsQueueSender
{

	private JmsTemplate jt;

	private Queue queue;

	public void simpleSend(final String input)
	{
		jt.send(queue, new MessageCreator()
		{
			public Message createMessage(Session session) throws JMSException
			{
				return session.createTextMessage(input);
			}
		});
	}

	public void setQueue(Queue q)
	{
		queue = q;
	}

	public void setJt(JmsTemplate jt)
	{
		this.jt = jt;
	}

	public Queue getQueue()
	{
		return queue;
	}

}

-- 
View this message in context: http://www.nabble.com/Question-about-unit-test-with-activemq-tf3820428s2354.html#a10852036
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Question about unit test with activemq

Posted by Bruce Snyder <br...@gmail.com>.
On 5/28/07, Carfield Yim <ca...@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/

Re: Question about unit test with activemq

Posted by Carfield Yim <ca...@carfield.com.hk>.


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
-- 
View this message in context: http://www.nabble.com/Question-about-unit-test-with-activemq-tf3820428s2354.html#a10831734
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Question about unit test with activemq

Posted by Carfield Yim <ca...@carfield.com.hk>.
> 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.

Re: Question about unit test with activemq

Posted by Bruce Snyder <br...@gmail.com>.
On 5/26/07, Carfield Yim <ca...@carfield.com.hk> wrote:
> I've create a testcase according to
> http://activemq.apache.org/how-to-unit-test-jms-code.html . However,
> after I send the message, how can I assert my message really sent?

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

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/