Mocked criteriaQuery return NullPointerException

4,853 views
Skip to first unread message

Marco Bellaiuto

unread,
Jun 29, 2016, 1:14:37 PM6/29/16
to mockito
I have to test my web application, testing persistence with arquillian i see it's very slow (400s for a test with 1 method), so I'm starting my business logic test using mockito. first of all i need to test .findAll()...

UserFacade extends AbstractFacade which have this findAll .


public abstract class AbstractFacade<T> {


   
private Class<T> entityClass;


   
public AbstractFacade(Class<T> entityClass) {
       
this.entityClass = entityClass;
   
}


   
protected abstract EntityManager getEntityManager();


   
....


   
public List<T> findAll() {
       
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
       
CriteriaQuery<T> cq = cb.createQuery(entityClass);
       
Root<T> rootEntry = cq.from(entityClass);
       
CriteriaQuery<T> all = cq.select(rootEntry);
       
TypedQuery<T> allQuery = em.createQuery(all);
       
return allQuery.getResultList();
   
}
   
...


My test:

@RunWith(MockitoJUnitRunner.class)
public class UserFacadeTest {


   
public UserFacadeTest() {
   
}
   
@Mock
   
private EntityManager mockedEM;
   
@Mock
   
private CriteriaQuery mockedCQ;
   
@Mock
   
private CriteriaQuery mockedCQAll;    
   
@Mock
   
private Root mockedRoot;
   
@Mock
   
private CriteriaBuilder mockedCB;
   
@Mock
   
private TypedQuery mockedTQ;
   
private UserFacade userFacade;
   
List<User> userList= new ArrayList<>();


   
@Before
   
public void setUp() {
        userFacade
=new UserFacade();
        userFacade
.setEm(mockedEM);
       
when(mockedEM.getCriteriaBuilder()).thenReturn(mockedCB);
       
when(mockedCB.createQuery()).thenReturn(mockedCQ);
       
when(mockedCQ.from(User.class)).thenReturn(mockedRoot);
       
when(mockedCQ.select(mockedRoot)).thenReturn(mockedCQAll);
       
when(mockedEM.createQuery(mockedCQAll)).thenReturn(mockedTQ);
       
when(mockedTQ.getResultList()).thenReturn(userList);
   
}


   
@Test
   
public void testFindAll() {
       
System.out.println("findAll");
       
User user = new User(1, "username", "password", "email", new Date());
        userFacade
.create(user);
        verify
(mockedEM, times(1)).persist(any());
        userList
.add(user);
       
//next line cause NullPointerException
       
List list=userFacade.findAll();


        assertTrue
("Should return 1", 1==list.size());


   
}

where i'm wrong?

David M. Karr

unread,
Jun 29, 2016, 1:21:20 PM6/29/16
to moc...@googlegroups.com
Run your test in the debugger and determine exactly which object is null.  That will lead you to the solution.  I sincerely doubt that "userFacade" is null, which you imply from your comment.

Message has been deleted
Message has been deleted
Message has been deleted

Marco Bellaiuto

unread,
Jul 2, 2016, 5:42:42 AM7/2/16
to mockito
after a debug i saw that "cq" is null... why?

maybe i'm wrong passing User.class... i have to mock this too?

Marco Bellaiuto

unread,
Jul 2, 2016, 7:03:44 AM7/2/16
to mockito
i was mocking it wrong...

when(mockedCB.createQuery()).thenReturn(mockedCQ);


So, when mockedCB.createQuery() is called, then mockedCQ will be returned. But then, in my code i have 
cb.createQuery(entityClass); which is not the same method. So i had to change it to

when(mockedCB.createQuery(any())).thenReturn(mockedCQ);
Reply all
Reply to author
Forward
0 new messages