Search code examples
javajunitjbossjmscdi

Is there a way to use JMS in a JUnit test?


I want to run a JUnit test with usage of JMS. Is it possible to have a JUnit test use JMS outside of an Application Server like JBoss or a CDI container?


Solution

  • Provided that sending and consuming the message is completely decoupled from JMS, you could mock it.

    For example: You can have a class that implements an interface like "IMyClassSender". In real code (non junit), all this class does is submit the message to JMS. In junit, implement IMyClassSender with a class that takes the input and instead if submitting to JMS, it passes it to your consumer class.

    Alternatively, if you are using active mq: http://activemq.apache.org/how-to-unit-test-jms-code.html