2016-05-29 6 views
0

私はJava EE 7でプロジェクトに取り掛かりました。[email protected] beanを別のものに注入する必要があります。どちらの豆は、類似した構造を持っている:@Stateless beanに注入するときに@Injectが失敗する

@Stateless 
public class OperationRepository extends GenericRepository<Operation> { 

    @PersistenceContext 
    private EntityManager entityManager; 

    public OperationRepository() { 
    } 
    /*Implementation of abstract methods, getters/setters, etc*/ 
} 

@Stateless 
public class MenuRepository extends GenericRepository<Menu> { 

    @PersistenceContext 
    private EntityManager entityManager; 

    @Inject 
    private OperationRepository operationRepository; 

    public MenuRepository() { 
    } 

    /*idem OperationRepository*/ 

    public List<Menu> getMenuFromOperation(...) { 
     // Do something where I need operationRepository 
    } 
} 

GenericRepository<E>は、いくつかの一般的な方法や他の抽象メソッドを持つだけの抽象クラスであるここでは関係ありません。

問題は、getMenuFromOperation()メソッドでNullPointerExceptionが発生することです。コードのデバッグ私は、注入されたoperationRepositoryがメソッドで要求されたときにnullであることを認識しました。

なぜ注入ポイントに失敗しますか?私はここで何が欠けているのですか?ほんの少しのテストを行うために

、私はあなたの答えを事前に

おかげで(nullである)MenuRepositoryコンストラクタでデフォルトOperationRepositoryをインスタンス化することによって、手動で注入され、その場合にはOperationRepository.entityManagerが注入されていません。

編集#1

John Amentが要求したとして、ここではそれが行く:

  1. すべての私のコードは、単一のjarのファイルです。これは、Glassfish Server 4.1にWebモジュール(warパッケージ)とともにデプロイされるMavenモジュールです。私はまだ開発していますので
  2. は、プロジェクトをデプロイする準備ができていないので、まだ、まだ存在しませんbeans.xml
  3. MenuRepository@Testクラスから利用され(私はまだ統合テストを実行しませんでした) MenuRepository

    public class MenuOperationRepositoryUTest extends BaseTestRepository { 
        private MenuRepository menuRepository; 
        private OperationRepository operationRepository; 
    
        @Before 
        public void initTestCase() { 
         initTestDB(); 
    
         menuRepository = new MenuRepository(); 
         menuRepository.setEntityManager(em); 
         operationRepository = new OperationRepository(); 
         operationRepository.setEntityManager(em); 
        } 
    
        @After 
        public void finalizeTestCase() { 
         closeEntityManager(); 
        } 
    
        /*Some successful tests*/ 
    
        @Test 
        public void showMenuFromOperation() { 
         // Insert some dummy data into the test DB (HSQL) 
    
         // This method needs the injected OperationRepository in MenuRepository 
         List<Menu> menu = menuRepository.getMenuFromOperation(...); 
    
         // Assertions 
        } 
    } 
    

    、次のようにBaseTestRepositoryは次のとおりです:次のよう

テストクラスのためのコードがある

@Ignore 
public class BaseTestRepository { 

    private EntityManagerFactory emf; 
    protected EntityManager em; 

    // This is a helper class that contains all the boilerplate to begin transaction 
    // and commit, it's used to insert data in the test DB 
    protected DBCommandExecutor dbCommandExecutor; 

    protected void initTestDB() { 
     // sigeaPU is the name declared in persistence.xml 
     emf = Persistence.createEntityManagerFactory("sigeaPU"); 
     em = emf.createEntityManager(); 

     dbCommandExecutor = new DBCommandExecutor(em); 
    } 

    protected void closeEntityManager() { 
     em.close(); 
     emf.close(); 
    } 
} 

は、私はそれは私がこれまでに得たすべてだと思います。

+0

インターフェイスで「@ Inject」を作成します。 – Geinmachi

+0

@LocalBeanをクラスに追加しようとしましたか? –

+1

@Geinmachi、私が理解しているように、Beanがローカルのときはインタフェースを省略できます。また、同じ構造で作業している他のプロジェクトも見てきましたが、そうではないと思います。プロジェクトが別の問題を起こす方法についての詳細が必要な場合は教えてください。 –

答えて

1

CDIコンテナをテストしているので、テストクラスの@Beforeメソッドで依存関係を手動で設定する必要があります。

menuRepository.setOperationRepository(operationRepository)

関連する問題