0

I have a servlet for which i am writing test case for , have it working already but at the end of the code some JMS calls are made which is throwing exception "javax.naming.NameNotFoundException" , i had this error for EJB beans as well and datasource , so i had them injected on @BeforeClass and they are working fine now i need same to done for JMS calls as follows.

my jms calls in servlet is as follows .

 ctx=new InitialContext();
        queueConnectionFactory=(QueueConnectionFactory) ctx.lookup("jms/queueConnectionFactory");
        queue=(Queue) ctx.lookup("jms/reportQueue");
        queueConnection=queueConnectionFactory.createQueueConnection();
        queueSession=queueConnection.createQueueSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
        queueSender=queueSession.createSender(queue);
        message=queueSession.createObjectMessage(etEDocRequestParams);
        queueSender.send(message);

But i don't know how JMS object can be inserted into Jndi without container. Please help.

My test @beforeclass is creating a fake InitialContext and it's working for getting DataSource and using EJBMOCK it's working for EJB as well. please see the code bellow.

 public class BookingSaveTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    ComboPooledDataSource ds = new  ComboPooledDataSource();
    ds.setDriverClass("com.evermind.sql.DriverManagerDataSource");
    ds.setJdbcUrl("jdbc:oracle:thin:@192.168.74.49:1521:xyz");
    ds.setUser("devpxyz");
    ds.setPassword("pass123");
    ds.setMaxPoolSize(1);
    ds.setMaxPoolSize(15);
    ds.setAcquireIncrement(3);
    ds.setMaxStatementsPerConnection(100);
    //ds.setAutomaticTestTable("c3p0_test_table");
    ds.setNumHelperThreads(20);
    SimpleNamingContextBuilder builder = null;
    builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();

    builder.bind("java:comp/env/jdbc/DB", ds);


     //bellow this is mocking EJB container
    MockContextFactory.setAsInitial();

    // Create an instance of the MockContainer and pass the JNDI context that 
    // it will use to bind EJBs. 
    MockContainer mockContainer = new MockContainer( new InitialContext() );


    SessionBeanDescriptor statefulSampleDescriptor = 
        new SessionBeanDescriptor( "PRQSessionBean", 
                PRQSessionHome.class, PRQSession.class, PRQSessionBean.class );
    // Mark this bean as stateful. Stateless is the default.
   statefulSampleDescriptor.setStateful( true );
    mockContainer.deploy( statefulSampleDescriptor );
}
 ..
 ..
 ..
  }
Vivek
  • 580
  • 2
  • 7
  • 26

2 Answers2

1

Your problem is not with JMS, but with JNDI. You need to register your JMS mock in the JNDI registry, which most likely isn't properly setup at all if you are testing outside an application server.

This question asks basically about the same problem and should help you solve the issue: Junit Testing JNDI InitialContext outside the application server

Community
  • 1
  • 1
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Thank you for your response Jens , link you posted is for Datasource , i am already able to create a fake InitialContext for that as well as fake container for EJB using EJBMock and they are working well, please look at my original post again , i have made some changes. – Vivek Apr 16 '14 at 07:42
  • In that case I don't really understand your problem. Just proceed like you did with the datasource: create a mock of the class you need and bind it to the jndi context as you do in your code. – Jens Schauder Apr 16 '14 at 07:50
  • i have not done JMS before and those are interfaces can't bind directly , that's why asking if anyone have done it. – Vivek Apr 16 '14 at 07:57
  • What do you mean by "can't bind directly"? Does you computer explode when you try it? Or does the earth stop to rotate? – Jens Schauder Apr 16 '14 at 07:58
  • thank you for your help Jens. i will post solution here when i find it :) – Vivek Apr 16 '14 at 08:07
  • Why don't you tell us what actually prevents you from binding a mocked implementation to your already existing mock jndi context so we can help? – Jens Schauder Apr 16 '14 at 08:14
  • QueueConnectionFactory object is created using lookup and i can't find anyway to get this without doing lookup and i am not running unitcases inside the servlet container i can't lookup i have to bind it without container ,in case of Datasource i did it using C3PO,in case of EJB i used EJBMOCK. i told at the question that i have already done it for DataSource yet you gave link of DataSource.Question is simple i have to bind QueueConnectionFactory outside of a container , and it's a interface i can't make it's object unless i write it's fake impl class but in this case it's not a good idea. – Vivek Apr 16 '14 at 10:40
  • So why don't you create QueueConnectionFactory? It is an interface with two methods. Should be a no-brainer to implement it or to mock it using the mocking framework of your choice. Color me stupid, but I still don't see, where the problem is. – Jens Schauder Apr 16 '14 at 12:36
  • i am not trying to mock the object i am trying to fake it , it's two different thing , it's ok Jens , i am glad you tried to help , thank you for that :) .. and i found the solution and have posted as well – Vivek Apr 16 '14 at 13:20
0

If you have to fake JMS , JNDI and EJB , mockEJB provide all this , i didn't read it completely , i thought it was just for EJB. you have Implementation classes of all the interfaces defined in it , you can bind them to your context easily. http://mockejb.sourceforge.net/javadoc/index.html

Vivek
  • 580
  • 2
  • 7
  • 26