2 Replies Latest reply on Oct 31, 2012 10:08 AM by jay shaughnessy

    Cannot lookup JNDI name when start managed container in Arquillian

    Hiep Le Canh Newbie

      In JBoss 7.0.2, I am using:

       

      Context ic = getContext();

      obj = ic.lookup("java:global/earName/jarName/beanName");   

      .........

       

      Below is getContext() method:

      Context getContext() throws NamingException {

          Hashtable<String, String> ht = new Hashtable<String, String>();

          ht.put(Context.INITIAL_CONTEXT_FACTORY,

                  "org.jboss.as.naming.InitialContextFactory");

          return new InitialContext(ht);

      }

       

      it works when I am using JBoss standalone (start manually from $JBOSS_HOME/bin/standalone.bat)

       

      But when I am using Arquillian to start managed jboss, I cannot get that object. (javax.naming.NameNotFoundException thrown).

      Any idea how to fix it highly appreciate.

       

      Thanks

        • 1. Re: Cannot lookup JNDI name when start managed container in Arquillian
          jay shaughnessy Expert

          Hi,

           

          I'm having the same issue.  I'm trying to lookup the TransactionManager outside of the container.  I'm using AS 7.1.1.Final and Arquillian 1.0.2.Final and testng,  Unfortunately it seems that Injection of the InitialContext, EntityManager, etc does not happen outside of @Test.  But I need to set some stuff up in @BeforeMethod.  I would have thought injection would extend to the BeforeMethod but it does not seem to, at least not at the moment.  I'm not sure if that is a bug or working as expected.

           

          But given that it's currently the case, is there a way to do JNDI lookups?  I haven't found the magic.  Here is roughly what I have inside the BeforeMethod:

           

          try {

            Properties env = new Properties();

            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");

            initialContext = new InitialContext(env);

           

            TransactionManager tm = (TransactionManager) getInitialContext().lookup("java:jboss/TransactionManager");

           

          } catch (Exception e) {

             ...

          }

           

          Which gives me: javax.naming.NameNotFoundException: java:jboss/TransactionManager

           

          Is there something else I can try?  Does this need to be a remote JNDI lookup?

           

          Thanks, Jay

          • 2. Re: Cannot lookup JNDI name when start managed container in Arquillian
            jay shaughnessy Expert

            This was due to the "BeforeMethod" called twice behavior for Arquillian and testNg.  By adding a check to ensure it was the in-container invocation this went away.  This can be done by doing something like:

             

            @ArquilianResource

            InitialContext initialContext;

             

            @BeforeMethod

            public void before() {

              if ( null == initialContext )

                  return;

             

              // do beforeMethod work

            }

             

             

            See https://community.jboss.org/thread/173265 for more