0
votes

Code from Enterprise JavaBeans 3.1 chapter 4. If you need more code or information just ask!

public class SimpleCalculatorIntegrationTest {

    private static SimpleCalculatorBean calc;
    private static Context namingContext;
    private static final String JNDI_NAME_CALC = "java:global/SimpleCalculatorEJB/SimpleCalculatorBean";

    @BeforeClass
    public static void obtainProxyReferences() throws NamingException {
        namingContext = new InitialContext();

        calc = (SimpleCalculatorBean) namingContext.lookup(JNDI_NAME_CALC);
    }

    @Test
    public void testAddition() {

        int expectedSum = 1 + 2 + 3 + 4; // 10

        assertEquals(expectedSum, calc.add(1, 2, 3, 4));
    }
}

Stacktrace:

11.okt.2011 20:41:28 com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient INFO: Cannot find javadb client jar file, derby jdbc driver will not be available by default. java.lang.RuntimeException: Orb initialization erorr at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:180) at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:365) at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:372) at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:402) at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455) at javax.naming.InitialContext.lookup(InitialContext.java:392) at no.breakpoint.ejbbook.calculator.test.SimpleCalculatorIntegrationTest.obtainProxyReferences(SimpleCalculatorIntegrationTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.NullPointerException at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:152) ... 23 more

1
Are you executing the test in the container (i.e. using Arquillian?). If you're running outside of the container be sure that you're not using a local or no-interface EJB view as you're trying to reach it from another JVM.Piotr Nowicki
@PedroKowalski Do you have 5 minutes to come on the Java chat? I really would appriciate some help so I can move on to the next chapter.LuckyLuke

1 Answers

4
votes

Just to sum up our chat conversation for other interested users:

You can write tests that works on your EJB's in two ways:

  1. Testing outside-of-the-container. It means that your tests run as a different application on different JVM. In this case you need to use the Remote interface EJB view and JNDI to locate your EJB. The JNDI coordinates are construct as written in EJB 3.1 specification regarding portable JNDI syntax (p. 81). In case of Glassfish you just need to:
    • add the remote interface to your classpath (to be able to use it),
    • add the gf-client.jar to your classpath.

Then you will be able to locate your EJB's remote interface by invoking code somewhat similar to this:

public class Main {

    private static final String JNDI = 
                    "java:global/yourApp/YourEJBBean!com.test.YourEJBBeanRemote";

    public static void main(String[] args) throws NamingException {
        Context ctx = new InitialContext();

        YourEJBBeanRemote sr = (YourEJBBeanRemote)ctx.lookup(JNDI);

        // Invoke some method on 'sr'
    }
}

2. Testing inside-of-the-container. It means that your tests are executed within the container and, very likely, together with your application. This allows you to use dependency injection, EntityManagers, local/no-interface EJB's view and so on. With JBoss Arquillian, you write your tests just assuming that all the services are provided for you.

The EJB 3.1 new no-interface view is just like local view, so it cannot be used for clients residing outside of the application.