0

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

fnlls10
  • 78
  • 1
  • 7
  • Can you please explain what "calling the method from the main" mean? Is this call made in the application server? – Priyesh May 13 '14 at 15:57
  • I call the method 'registrazione' in the the main method in a class that is located in the Source Package of the application client. So we success in using the session bean outside the application server, without specifying the jndi properties. I don't undestand why if I use the same code in a test class in test package it doesn't work. – fnlls10 May 13 '14 at 17:31
  • Can you share code from the main method? – Priyesh May 14 '14 at 09:15
  • @Priyesh I updated the post with the main method code. The main and the 'registrazione' test are located in the application client. Here's the project explorer. http://imagizer.imageshack.us/a/img837/1621/a21c.png – fnlls10 May 14 '14 at 10:32

1 Answers1

0

When you create an InitialContext using new InitialContext() outside the application server, it expects the following properties to be configured based on your application server:

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)
You can specify these as System properties or in a jndi.properties in your classpath.

Priyesh
  • 2,041
  • 1
  • 17
  • 25