spotten, wenn ich versuche, folgende Methode zu verspotten für den JUnit-Test (Methode Remote EJB Aufruf für Business-Logik verwendet), gibt es javax.naming.NoInitialContextExceptionWie Initial Konstruktor in Unit-Tests
private void someMethod(int id1, int id2, HashMap map){
......some code........
Context ctx = new InitialContext();
Object ref = ctx.lookup("com.java.ejbs.MyEJB");
EJBHome ejbHome = (EJBHome)PortableRemoteObject.narrow(ref, EJBHome.class);
EJBBean ejbBean = (EJBBean)PortableRemoteObject.narrow(ejbHome.create(), EJBBean.class);
ejbBean.someMethod(id1,name);
.......some code.......}
Meine Einheit Test für obige Verfahren
@Test
public void testsomeMethod() throws Exception {
.......setting initial code...
//Mock context and JNDI
InitialContext cntxMock = PowerMock.createMock(InitialContext.class);
PowerMock.expectNew(InitialContext.class).andReturn(cntxMock);
expect(cntxMock.lookup("com.java.ejbs.MyEJB")).andReturn(refMock);
..........some code..........
PowerMock.replayAll();
Whitebox.invokeMethod(ObjectOfsomeMethodClass, "someMethod", id1, id2, map);
}
wenn die Whitebox.invokeMethod (ObjectOfsomeMethodClass, "irgendeinemethode", ID1, ID2, Karte) Methode ruft es gibt Ausnahme folgen.
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(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
glaube ich, obwohl wir den Context in Testmethode verspotten, ist es nicht das Mock-Objekt nicht verwendet beim Aufruf Whitebox.invokeMethod (ObjectOfsomeMethodClass, "irgendeinemethode", ID1, ID2, Karte) Verfahren, statt dessen versucht es, den Kontext aufzurufen ctx = new InitialContext(); Methode in der ursprünglichen Methode (someMethod).
Sie könnten eine in-memory InitialContextFactory in Ihre Tests hinzufügen, Beispiel: http://StackOverflow.com/questions/3461310 –