我在我的机器上运行了 eclipse juno 和 jboss AS 7.1 final 并通过它进行了配置(eclipse juno)。我有一个简单的无状态 bean 及其接口(实际上与 Enterprise Java Beans 3.1 6th Edition 书中的示例完全相同)。这是代码:
package beans;
public class CalculatorBeanBase implements CalculatorCommonBusiness{
public int add(int[] opers){
int sum = 0;
for(int c:opers){
sum+=c;
}
return sum;
}
}
package beans;
public interface CalculatorCommonBusiness {
public int add(int[] opers);
}
package beans;
public interface CalculatorRemoteBusiness extends CalculatorCommonBusiness {}
package beans;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
@Stateless
@LocalBean
public class SimpleCalculatorBean extends CalculatorBeanBase {}
我正在尝试进行以下 JUnit 测试,不幸的是,该示例提到“使用我们假设在类路径上呈现的标准 jndi.properties 文件”(这是我的第一个问题,我不知道这意味着什么,我事实上做了一些研究,但我不太明白它是如何工作的,让我感到困惑,最终似乎无法让它发挥作用)。
package beans;
import javax.naming.Context;
import javax.naming.InitialContext;
import junit.framework.TestCase;
import org.junit.BeforeClass;
import org.junit.Test;
public class CalculatorIntegrationTestCase {
private static Context namingContext;
private static SimpleCalculatorBean cal;
private static final String JNDI_NAME_CALC = "java:global/jndi.properties/SimpleCalculatorBean";
@BeforeClass
public static void obtainProxyReferences() throws Throwable {
namingContext = new InitialContext();
cal = (SimpleCalculatorBean)namingContext.lookup(JNDI_NAME_CALC);
}
private void assertAdditionSucceeds(SimpleCalculatorBean cal){
final int[] oper = {2,3,4};
final int expectedSum = 9;
final int actualSum = cal.add(oper);
TestCase.assertEquals("Assert Failed!!. The actual value is not the expected",expectedSum, actualSum);
}
@Test
public void testAdditionUsingBusinessReference() throws Throwable {
this.assertAdditionSucceeds(cal);
}
}
我需要知道那个jndi。属性文件的工作原理以及我必须如何设置它。(我阅读了最后一个问题,我得到了以下 .txt 文件并将其扩展名更改为 .jar,我不知道这是否正确):
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jpn://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
最后,我将该 .jar 文件放入类路径中(不知道我是否做得对:右击项目、构建路径、库、添加外部 jar)。
我做了以上所有的事情,但我不断得到以下内容:
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
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at beans.CalculatorIntegrationTestCase.obtainProxyReferences(CalculatorIntegrationTestCase.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
当然,我发布了项目(尽管通过 eclipse,项目>运行>在服务器上运行)。帮助将不胜感激