我已经在我的机器上运行了juno和jboss AS 7.1,并通过它进行了配置(eclipse juno)。我有一个简单的无状态bean及其接口(事实上,它与Enterprise Java Beans 3.1第6版中的完全相同)。这是代码:
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,项目>运行>在服务器上运行)。帮助将不胜感激
答案 0 :(得分:0)
AS7有一个全新的远程ejb / jndi实现,这意味着你不能再使用jnp了。
看看https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
您可以在那里找到如何操作的说明。
答案 1 :(得分:0)
检查jndi.properties文件中的命名提供程序URL。
它应该是jnp://localhost:1099