4 Replies Latest reply on Sep 8, 2006 4:35 PM by treespace

    JUnit testing EJBs: NoInitialContextException

    Bernhard Humm Newbie

      Dear all,

      I want to write JUnit Tests for my EJB3.0 SessionBeans. How do I access the application server?

      InitialContext ctx = new InitialContext();
      bean = (BeanInterface) ctx.lookup("...");

      does not work since the initial context cannot be resolved (javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial).

      Obvoiusly, for the same reason, dependency injection doesn't work either:
      @EJB
      BeanInterface bean;

      What do I have to do?

      Best regards
      Bernhard

      --
      Prof. Dr. Bernhard Humm
      Darmstadt University of Applied Sciences
      b.humm@fbi.h-da.de
      www.fbi.h-da.de/~b.humm


        • 1. Re: JUnit testing EJBs: NoInitialContextException
          Peter Johnson Master

          Create a jndi.properties file, place it in the classpath (usually in the base directory of the jar file), with these contents:

          java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
          java.naming.provider.url=localhost:1099
          java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces


          Of course, replace locahost with your host name if you are attempting remote access.

          You can also provide those same properties within the application by creating a hash map and passing it to the InitialContext constructor.

          • 2. Re: JUnit testing EJBs: NoInitialContextException
            Andrew Rubinger Master

            From within the container, your InitialContext will be configured properly when using the default constructor. From a remote client (another JVM, like your JUnit Test), this isn't the case, and you'll have to get your context:

            Hashtable<String, String> props= new Hashtable<String, String>();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            props.put(Context.PROVIDER_URL,"jnp://localhost:1099" );
            props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces" );
            Context ctx = new InitialContext( props );
            


            ..replacing the host and port in Context.PROVIDER_URL with something appropriate.

            Dependency injection...you'll have to do a lookup for that bean using the Context above - you'll only be able to inject container-managed resources into other container-managed resources.

            S,
            ALR

            • 3. Re: JUnit testing EJBs: NoInitialContextException
              Alan Berezin Newbie

              Also, for a remote client junit test, I didnt see a jboss client jar in the lastest 4.04ga release. I found I had to add a bunch of jboss jars to the client runtime classpath. I cant recall what all i added but if it still a problem, I can find out and post another response.

              • 4. Re: JUnit testing EJBs: NoInitialContextException
                treespace Novice

                Cactus provides an excellent in-container test facility:

                http://jakarta.apache.org/cactus/

                I am using with JBoss 4.X to test session and by extension entity beans.