I'm developing a client-server application using EJB and Glassfish in NetBeans. I've implemented a method on the client that uses a Session Bean. I've tried the method calling the method from the main and it works. Now I was trying to test the behavior of the method in a test case. Netbeans force me to put the JUnit test case in the Test Packages folder. Running the test throws the following exception:
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)
Here is the client method 'registrazione' that i want to test:
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);
}
}
Note that the same method called from the main in the sources packages doesn't throws this exception and works fine. Here's the code:
public class Main {
public static void main(String[] args) throws VincoliInputException, RegistrazioneException, CategoriaGiaEsistenteException {
GestoreAccountLocale.registrazione("user123", "passw123", "John", "Doe", "johndoe@gmail.com", new Date());
}
}
I suppose it's a problem of calling the Session Bean from the Test Packages folder in the application client. Any idea for solving this problem? Thanks