运行集成测试 EJB 时出现问题

发布于 2024-12-09 13:40:28 字数 3195 浏览 0 评论 0原文

代码来自 Enterprise JavaBeans 3.1 第 4 章。如果您需要更多代码或信息,请询问!

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));
    }
}

堆栈跟踪:

11.okt.2011 20:41:28 com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient 信息:找不到 javadb 客户端 jar 文件,derby jdbc 默认情况下驱动程序不可用。 java.lang.RuntimeException: Orb 初始化错误 org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:180) 在 com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:365) 在 com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:372) 在 com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:402) 在 com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347) 在 com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504) 在 com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455) 在 javax.naming.InitialContext.lookup(InitialContext.java:392) 处 no.breakpoint.ejbbook.calculator.test.SimpleCalculatorIntegrationTest.obtainProxyReferences(SimpleCalculatorIntegrationTest.java:24) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 处 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:236) 处 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 导致:java.lang.NullPointerException at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:152) ... 23 更多

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2024-12-16 13:40:28

总结一下我们与其他感兴趣的用户的聊天对话:

您可以通过两种方式编写适用于您的 EJB 的测试:

  1. 容器外测试。这意味着您的测试作为不同的应用程序在不同的 JVM 上运行。在这种情况下,您需要使用远程接口EJB视图和JNDI来定位您的EJB。 JNDI 坐标的构造如 EJB 3.1 规范 关于可移植 JNDI 语法(第 81 页)。如果是 Glassfish,您只需要:
    • 将远程接口添加到您的类路径中(以便能够使用它),
    • 将 gf-client.jar 添加到您的类路径中。

然后,您将能够通过调用与此类似的代码来定位 EJB 的远程接口:

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. 测试容器内部。这意味着您的测试是在容器内执行的,并且很可能与您的应用程序一起执行。这允许您使用依赖项注入、EntityManager、本地/无接口 EJB 的视图等。使用JBoss Arquillian,您编写测试时假设所有服务都为您提供。

EJB 3.1 新的无接口视图就像本地视图一样,因此它不能用于驻留在应用程序外部的客户端。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文