You are on the right track. There are several ways to handle this. Here are some tips for unit testing with OpenEJB and Maven.
Beans test
You can write all kinds of EJBs and other testing utilities and deploy them. All you need is ejb-jar.xml for the test code:
As usual, the ejb-jar.xml file should contain <ejb-jar/> and nothing else. Its existence simply tells OpenEJB to check this part of the classpath and scan it for beans. Scanning the entire classpath is very slow, so this is just an agreement to speed it up.
TestCase Insert
With src/test/resources/ejb-jar.xml above, you can easily add this test MDB and configure it to handle the request in the way that TestCase requires. But src/test/resources/ejb-jar.xml also opens up some other interesting features.
You could make TestCase yourself by declaring links to all the JMS resources you need and posting them.
import org.apache.openejb.api.LocalClient; @LocalClient public class ChatBeanTest extends TestCase { @Resource private ConnectionFactory connectionFactory; @Resource(name = "QuestionBean") private Queue questionQueue; @Resource(name = "AnswerQueue") private Queue answerQueue; @EJB private MyBean myBean; @Override protected void setUp() throws Exception { Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); InitialContext initialContext = new InitialContext(p); initialContext.bind("inject", this);
Now you are just one thread away from responding to the test JMS message itself. You can run a small runnable that will read a single message, send the answer you want, and then exit.
Maybe something like:
public void test() throws Exception { final Thread thread = new Thread() { @Override public void run() { try { final Connection connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final MessageConsumer incoming = session.createConsumer(requestQueue); final String text = ((TextMessage) incoming.receive(1000)).getText(); final MessageProducer outgoing = session.createProducer(responseQueue); outgoing.send(session.createTextMessage("Hello World!")); } catch (JMSException e) { e.printStackTrace(); } } }; thread.setDaemon(true); thread.start(); myBean.doThatThing();
Cm.
Alternative Descriptors
If you want to use the MDB solution and want to enable it for only one test, and not for all tests, you can define it in a special file src/test/resources/mockmdb.ejb-jar.xml and enable it in a specific test case, where necessary.
For more information on how to enable this descriptor and various alternative descriptor options, see this document .