|
When unit testing code with JMS you'll typically want to avoid the overhead of running separate proceses; plus you'll want to increase startup time as fast as possible as you tend to run unit tests often and want immediate feedback. Also persistence can often cause problems - as previous test case results can adversely affect future test case runs - so you often need to purge queues on startup. So when unit testing JMS code we recommend the following
You can do all of this using the following Java code to create your JMS ConnectionFactory which will also automatically create an embedded broker ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); For more configuration options see the VM Transport Reference and the Broker Configuration URI Or if you really would rather be more explicit you can create the broker first using the following Java code BrokerService broker = new BrokerService(); broker.setPersistent(false); broker.start(); or you could use the Spring Support Using JNDIIf your application code is using JNDI to lookup the JMS ConnectionFactory and Destinations to use, then you could use the JNDI Support in ActiveMQ. Add the following jndi.properties to your classpath (e.g. in src/test/resources if you are using maven). java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = vm://localhost?broker.persistent=false
You should then consider using Dynamic destinations in JNDI so that your code looks up destinations via context.lookup("dynamicQueues/FOO.BAR");
To avoid having to explicitly configure every single JMS destination in the jndi.properties file, please see the other options for creating destinations. |