【问题标题】:Creation of context fail when JUNIT test is launched启动 JUNIT 测试时创建上下文失败
【发布时间】:2011-10-23 21:58:39
【问题描述】:

我创建了一个从我的应用程序客户端启动的小型 junit 测试。 我的服务器是 glassfish 3.1.1 。 当我运行测试时,我得到了这个错误:

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

我已经将属性添加到上下文中,但没有任何改变。 这就是我设置上下文的方式:

@Before
    public void setUp() throws NamingException {
        Properties props = new Properties();
        System.out.println("launch");
        props.setProperty("java.naming.factory.initial",
                "com.sun.enterprise.naming.SerialInitContextFactory");

        props.setProperty("java.naming.factory.url.pkgs",
                "com.sun.enterprise.naming");

        props.setProperty("java.naming.factory.state",
                "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");


        // optional.  Defaults to localhost.  Only needed if web server is running
        // on a different host than the appserver   
        //props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");

        // optional.  Defaults to 3700.  Only needed if target orb port is not 3700.
        //props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
        Context annuaire = new InitialContext(props);
        GeoBeanRemote service = (GeoBeanRemote) annuaire.lookup(GeoBeanRemote.class.getName());
        BeanProvider.setGeoBean(service);
    }

app-serv.rt 和 gf-client 已添加到我的运行测试中。

谢谢。

【问题讨论】:

  • 如果您使用的是 GlassFish v3+,您的类路径中应该只需要 gf-client.jar
  • 你能显示异常堆栈跟踪吗?

标签: java jakarta-ee junit glassfish ejb-3.0


【解决方案1】:

当我的类路径中缺少 jndi.properties 时,我遇到了同样的问题。

附录: 我在这里感觉有点傻,但我认为你不能那样设置你的初始上下文。我注意到虽然 JNDI 属性甚至不需要在 Glassfish 上设置,所以让我感到困惑。

【讨论】:

  • 通过代码放置属性或将它们写入 jndi.properties 文件并加载它们有什么区别?你能粘贴你的 jndi.properties 吗?
猜你喜欢
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
相关资源
最近更新 更多