activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Carfield Yim <carfi...@carfield.com.hk>
Subject Re: Question about unit test with activemq
Date Tue, 29 May 2007 11:14:29 GMT


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.


Mime
View raw message