2016-03-22 18 views
1

私はmockitoを使用してテストしようとしているメソッドがあります。以下は私がテストしようとしている方法です。テストを実行すると、nullポインタ例外が発生します。私はrootを嘲笑していますが、accountEntity.getではnullポインタを取得しています。私はここで何が欠けていますか?条件照会Mockitoユニットテスト - NullPointerException

 public List<AccountEntity> createCriteriaQuery(final List<String> accountIdList, 
        final MultiValueMap<String, String> allRequestParams) { 

       final CriteriaBuilder cb = entityManager.getCriteriaBuilder();   
       final CriteriaQuery<AccountEntity> cq = cb.createQuery(AccountEntity.class); 
       final Root<AccountEntity> accountEntity = cq.from(AccountEntity.class); 

       final Join<AccountEntity, PlanEntity> account = accountEntity.join(AccountEntity_.planEntity); 

       final List<Predicate> predicates = new ArrayList<Predicate>(); 

     //Getting null pointer at the below call.   

**predicates.add(accountEntity.get(AccountEntity_.id).in(accountIdList));** 

    /*remaining code here/ 

以下は私のテストです。

@InjectMocks 
private AccountsDAO       accountsDao; 
@Mock 
EntityManagerFactory      entityManagerFactory; 
@Mock 
EntityManager        entityManager; 
@Mock 
CriteriaBuilder        cb; 
@Mock 
CriteriaQuery<AccountEntity>    cq; 
@Mock 
Root<AccountEntity>       rootAccountEntity; 
    @Test 
     public void canGetAllAccountsInfo() { 
     when(entityManagerFactory.createEntityManager()).thenReturn(entityManager); 
     when(entityManager.getCriteriaBuilder()).thenReturn(cb); when(cb.createQuery(AccountEntity.class)).thenReturn(cq); 
     when(cq.from(AccountEntity.class)).thenReturn(rootAccountEntity); 
//Null pointer in the actual method call 
      accountEntityList = accountsDao.createCriteriaQuery(accounIdList, allRequestParams);   
      } 

答えて

0

私はこの文

when(accountEntity.get(AccountEntity_.id)).thenReturn(path); 

を欠けていた私は、この

が必要

predicates.add(accountEntity.get(AccountEntity_.id).in(accountIdList)) 

を渡すために、このための実際のコードでテストクラス

@Mock 
    Path<String>     path; 

でこれを追加しました

when(accountEntity.get(AccountEntity_.id)).thenReturn(path); 
1

私はあなたが遊びに嘲笑accountEntityオブジェクトの任意のwhen(...).thenReturn(...)を追加していないので、accountEntity.get(AccountEntity_.id)nullに解決されることを推測します。

+0

私はそれを試みましたが、同じ問題がまだあります。 – Arun

関連する問題