0

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);
    }
}

ソース パッケージのメインから呼び出された同じメソッドは、この例外をスローせず、正常に動作することに注意してください。コードは次のとおりです。

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 を呼び出す問題だと思います。この問題を解決するためのアイデアはありますか? ありがとう

4

1 に答える 1