10 Replies Latest reply on Mar 26, 2009 7:57 AM by Sandor Fiedler

    Unit test EJB3 Exception

    Marcos Vinicius Newbie

      Hello people! I'm having trouble on EJB3 unit test.

      I'm getting a class cast exception when i try to retrieve the local bean interface for unit testing. My code is the following:

      // configure the enviroment

      Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      env.put(Context.PROVIDER_URL, "localhost:1099"); env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming.org.jnp.interfaces" );
      this.ctx = new InitialContext(env);

      //get the context

      MyLocal local = (MyLocal)PortableRemoteObject.narrow(ctx.lookup("MyBean/local"), MyLocal.class);//-> here i gert the exception


      And the exception is that below

      java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.omg.CORBA.Object

      So please if you have experienced the same problem, tell me about.

      Thanks!!

        • 1. Re: Unit test EJB3 Exception
          Wolfgang Knauf Master

          Hi,

          what does the JNDIView ( http://www.jboss.org/community/docs/DOC-9583 ) display for your bean and for the app client?

          According to the message, I would suspect that the JNDI path continues (something like "MyBean/local/somethingmore"), and your lookup is not done on the end node of the path.
          Or did you configure the local interface name, which is a reference to something else?

          Hope I could help

          Wolfgang

          • 2. Re: Unit test EJB3 Exception
            Marcos Vinicius Newbie

            I think the problem is not there, because I can get the Reference to MyBean with

            ctx.lookup("MyBean/local")

            The problem is when i try to narrow this with

            MyBeanLocalInterface i = PortableRemoteObject.narrow(ref, MyBean.class)

            Propably i'm missing some jar in the classpath. The server i'm using is JBoss.


            Thanks for the attention!

            • 3. Re: Unit test EJB3 Exception
              Wolfgang Knauf Master

              Oops, my bad... Local interfaces can be cast directly:

              MyLocal local = (MyLocal) ctx.lookup("MyBean/local");


              Probably a "narrow" would even result in error.

              By the way, with EJB3, the "narrow" is even for remote interfaces no longer required, direct casting is possible.

              And one more thing: a Unit test probably means that you are using a remote application client? Then, shouldn't you use the remote interface?

              Best regards


              Wolfgang

              • 4. Re: Unit test EJB3 Exception
                Marcos Vinicius Newbie

                Hummm I changed the code to:

                MyLocal local = (MyLocal)ctx.lookup("MyBean/local");

                But I am now getting the exception:


                java.lang.ClassCastException: javax.naming.Reference cannot be cast to br.com.original.negocio.email.interfaces.IEmailLocal

                Thanks!

                • 5. Re: Unit test EJB3 Exception
                  Andrew Rubinger Master

                   

                  "mvpc" wrote:
                  java.lang.ClassCastException: javax.naming.Reference cannot be cast to br.com.original.negocio.email.interfaces.IEmailLocal


                  This is a common symptom that you don't have the proxy object factories on your client CP. Typically this is fixed by ensuring you've got $JBOSS_HOME/client/jbossall-client.jar defined. But I see some other issues.

                  * Your Unit Test is set up in a different JVM from the container? If so you'll need to lookup a business remote reference (ie. @Remote).
                  * Don't push local component or local business proxies through PortableRemoteObject.narrow...this is for the EJB 2.x remote home view only.

                  S,
                  ALR

                  • 6. Re: Unit test EJB3 Exception
                    Marcos Vinicius Newbie

                    I use the same JVM that the EJB is deployed and have added the jbossall-cliente.jar and it doesn't work at all, I get the same exception.

                    My EJB application is running when i execute the unit test. Is This right ?


                    • 7. Re: Unit test EJB3 Exception
                      Wolfgang Knauf Master

                      Hi,

                      do you use JBoss 5? If yes: "jbossall-client.jar" just defines a bunch of classpath entries in its Manifest.mf, so you have to add much more JARs to your classpath (but I cannot tell you, which ones).

                      And one more thing: as your unit test seems to be a standalone application client, you have to use the REMOTE interface, not the local one (as ALRubinger already told you).

                      Hope this helps

                      Wolfgang

                      • 8. Re: Unit test EJB3 Exception
                        Wolfgang Knauf Master

                        What do you mean with "I use the same JVM..." in your previous post? Same java.exe executable, or same process?
                        If the latter: how do you start the Unit Tests so that they run inside the container?

                        Wolfgang

                        • 9. Re: Unit test EJB3 Exception
                          Marcos Vinicius Newbie

                          I just meant that i am executing the unit tests on same machine that the EJB is deployed. Really I use JBoss 5 and i think there are lots of jars more to add to classpath. And I have to use local interface, i can't use remote interface in a application that's running locally.


                          Thanks! I'll search the jars :)

                          • 10. Re: Unit test EJB3 Exception
                            Sandor Fiedler Newbie

                            In case of an test client startet with an own main method this client runs in fact in a different process then the jboss and your ejbs. In that case you must use the remote interfaces as per the process is remote to the jboss (as already Wolfgang and ALR said).