我有eclipse juno和jboss AS 7.1最後在我的機器上運行並通過它配置它(eclipse juno)。我有一個簡單的無狀態bean和它的接口(實際上與Enterprise Java Beans 3.1第六版完全一樣)。下面是代碼:一步一步jboss 7.1最終jndi屬性文件創建
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 {}
我試圖做的FOL降低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。屬性文件的工作原理以及我如何設置它。 (我讀到這最後一個問題,我得到了folloing .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文件到類路徑(不知道我是否做得正確:在項目上按鼠標右鍵,建立路徑,庫,添加外部罐子)。
我做了所有上述的東西,但我不斷收到如下:
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)
當然我發表的項目(儘管通過月食,項目>運行>在服務器上運行)。幫助,將不勝感激
所以基本上我必須改變這一點:import javax.naming.Context; import javax.naming.InitialContext; import junit.framework.TestCase; import org.junit.BeforeClass; import org.junit.Test;對此:java.naming.factory.initial = org.jboss.naming.remote.client.InitialContextFactory java.naming.provider.url = remote:// localhost:4447 ??順便謝謝你的回答 –
@JonathanIbanez它是否適合你?不適合我。 – theV0ID
@ theV0ID不,它沒有。很久以前,我嘗試了很長一段時間,現在我正在做其他事情,抱歉我無能爲力。我認爲使用屬性文件而不是最終的靜態變量可以完成這項工作,當然我們仍然需要使用正確的屬性,但是,我不太確定這些是什麼。讓我知道你的成就。 –