我正在NetBeans中使用EJB和Glassfish开发客户机-服务器应用程序。我在客户机上实现了一个使用会话Bean的方法。我已经尝试了从主调用方法的方法,它的工作。现在我试着在测试用例中测试方法的行为。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());
}
}
我想这是一个从应用程序客户端的Test Packages文件夹中调用Session Bean的问题。有解决这个问题的办法吗?由于
当您在应用服务器外部使用new InitialContext()
创建InitialContext时,它期望根据您的应用服务器配置以下属性:
java.naming.factory.initial (Factory specific to your application server)
java.naming.provider.url (URL of the application server e.g. t3://localhost:7003)
java.naming.security.principal (username)
java.naming.security.credentials (password)
(http://docs.oracle.com/javase/7/docs/api/javax/naming/Context.html # INITIAL_CONTEXT_FACTORY)
您可以将它们指定为系统属性或在类路径中的jndi.properties
中。