我正在使用 NetBeans 中的 EJB 和 Glassfish 开发客户端-服务器应用程序。我已经在使用会话 Bean 的客户端上实现了一个方法。我已经尝试过从 main 调用该方法的方法,它可以工作。现在我试图在测试用例中测试该方法的行为。Netbeans 强迫我将 JUnit 测试用例放在 Test Packages 文件夹中。运行测试会引发以下异常:
Testcase: test21(registrazioneTest): Caused an ERROR
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
java.lang.RuntimeException: 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 GestoreAccountLocale.GestoreAccountLocale.lookupGestoreAccountRemotoRemote(GestoreAccountLocale.java:147)
at GestoreAccountLocale.GestoreAccountLocale.registrazione(GestoreAccountLocale.java:37)
at registrazioneTest.test21(registrazioneTest.java:185)
Caused by: 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:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at GestoreAccountLocale.GestoreAccountLocale.lookupGestoreAccountRemotoRemote(GestoreAccountLocale.java:144)
这是我要测试的客户端方法“registrazione”:
public static void registrazione(String username, String password, String nome, String cognome, String email, Date dataNascita) throws VincoliInputException, RegistrazioneException {
checkUsername(username);
checkPassword(password);
checkNome(nome);
checkCognome(cognome);
checkEmail(email);
checkData(dataNascita);
GestoreAccountRemotoRemote gestore = lookupGestoreAccountRemotoRemote();
if(! gestore.verificaDatiAccount(username,password,nome,cognome,email,"dataNascita")) {
System.out.println("Errore nella registrazione");
throw new RegistrazioneException();
}
System.out.println("Registrazione avvenuta con successo.");
}
private static GestoreAccountRemotoRemote lookupGestoreAccountRemotoRemote() {
try {
Context c = new InitialContext();
//return (GestoreAccountRemotoRemote) c.lookup("java:global/ServerMDB/ServerMDB-ejb/GestoreAccountRemoto");
return (GestoreAccountRemotoRemote) c.lookup("java:global/ServerMDB/ServerMDB-ejb/GestoreAccountRemoto!GestoreAccountRemoto.GestoreAccountRemotoRemote");
} catch (NamingException ne) {
//Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
请注意,从源包中的 main 调用的相同方法不会引发此异常并且工作正常。这是代码:
public class Main {
public static void main(String[] args) throws VincoliInputException, RegistrazioneException, CategoriaGiaEsistenteException {
GestoreAccountLocale.registrazione("user123", "passw123", "John", "Doe", "johndoe@gmail.com", new Date());
}
}
我想这是从应用程序客户端的测试包文件夹中调用会话 Bean 的问题。有解决这个问题的想法吗?谢谢