Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:03:52+00:00 2026-05-25T15:03:52+00:00

I have a EJB to send a message to JMS queue and wait the

  • 0

I have a EJB to send a message to JMS queue and wait the reply from it. I want to test the EJB, it’s easy to use OpenEJB to do the JUnit test of the EJB. But the problem is this EJB will wait the JMS response to continue process.

Although I can send message in my junit code, but because the EJB is still on-going, I cannot run it before the EJB is completed.

2nd solution is I can initialize a MDB to listen and reply the JMS message form the EJB, but the problem is the MDB must in src\main\java and cannot in src\test\java. The problem is this is just a test code and I should not package it to production environment. (I use Maven)

Or should I use mock object ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:03:52+00:00Added an answer on May 25, 2026 at 3:03 pm

    You’re on the right track. There area few ways to handle this. Here are a couple tips for unit testing with OpenEJB and Maven.

    Test beans

    You can write all sorts of EJBs and other testing utilities and have them deployed. All you need is a ejb-jar.xml for the test code like so:

    • src/main/resources/ejb-jar.xml (the normal one)

    • src/test/resources/ejb-jar.xml (the testing beans)

    As usual the ejb-jar.xml file only needs to contain <ejb-jar/> and nothing more. Its existence simply tells OpenEJB to inspect that part of the classpath and scan it for beans. Scanning the entire classpath is very slow, so this is just convention to speed that up.

    TestCase injection

    With the above src/test/resources/ejb-jar.xml you could very easily add that test-only MDB and have it setup to process the request in a way that the TestCase needs. But the src/test/resources/ejb-jar.xml also opens up some other interesting functionality.

    You could have the TestCase itself do it by declaring references to whatever JMS resources you need and have them injected.

    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); // here's the magic!
        }
    }
    

    Now you’re just one thread away from being able to respond to the JMS message the testcase itself. You can launch off a little runnable that will read a single message, send the response you want, 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();
    
        // asserts here...
    }
    

    See

    Alternate Descriptors

    If you did want to use the MDB solution and only wanted to enable it for just the one test and not all tests, you could define it in a special src/test/resources/mockmdb.ejb-jar.xml file and enable it in the specific test case(s) where it is needed.

    See this doc for more information on how to enable that descriptor and the various options of alternate descriptors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a public EJB class that I want accessible online as a web
I am facing another problem in my little test-webapp. I have an EJB module
I have been struggling trying to test a super simple EJB project in netbeans.
I have an @Stateless EJB method in which I delete some entries from a
I have a remote EJB with a method that validates an object (returning true
I have an old EJB (2.1) project that uses xdoclet (1.2.3) to generate the
We have some JavaEE5 stateless EJB bean that passes the injected EntityManager to its
I have a large application that uses EJB 2.x entity beans (BMP). This is
Guys, I HAVE tried reading tons of stuff about EJB. And I don't get
I have a EJB module in remote Glassfish server and application client in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.