I have some hibernate methods and when i test them, mockito always return null instead of expected value
This is my hibernate method
@Autowired
private SessionFactory sessionFactory;
public StudentDAO() {
}
public List<StudentDetail> listStudentDetail() {
String hql = "Select new " + StudentDetail.class.getName() //
+ "(s.studentid,s.name,s.address) " //
+ " from " + Student.class.getName() + " s ";
Session session = this.sessionFactory.getCurrentSession();
Query<StudentDetail> query = session.createQuery(hql, StudentDetail.class);
List<StudentDetail> list =null;
list = query.getResultList();
return list;
}
And this is my test method
@Mock
SessionFactory sessionFactory;
@Mock
Query query;
@Mock
Session session;
@Mock
List<StudentDetail> list;
@InjectMocks
private StudentDAO studentDAO ;
// Test get method
@Test
public void getListStudentTest() {
StudentDetail sd1 = new StudentDetail(1, "A", "X");
Mockito.when(query.getResultList()).thenReturn(list);
Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
Mockito.when(session.createQuery(ArgumentMatchers.anyString())).thenReturn(query);
Mockito.verify(sessionFactory.getCurrentSession());
Mockito.verify(session.createQuery(ArgumentMatchers.anyString(),ArgumentMatchers.anyObject() ));
Mockito.verify(query.getResultList());
Assert.assertEquals(sd1, studentDAO.listStudentDetail().get(0));
}
When i run my web app, studentDAO.listStudentDetail()
return true value normally but in test method its always return null
Here
Assert.assertEquals(sd1, studentDAO.listStudentDetail().get(0));
the studentDAO.listStudentDetail()
returns a mock list. You didn't mock its .get
method. Thus it returns null
by default. You should modify your assertEquals
check:
Assert.assertEquals(list, studentDAO.listStudentDetail());
(You don't need the sd1
object at all)
Also you are not using verify
properly. You should do like this:
Mockito.verify(sessionFactory).getCurrentSession();
Mockito.verify(session).createQuery(ArgumentMatchers.anyString());
Mockito.verify(query).getResultList();
(note the closing parenthesis before .
)
Take a look at method that you mock
Mockito.when(session.createQuery(ArgumentMatchers.anyString())).thenReturn(query);
and method that you call:
Query<StudentDetail> query = session.createQuery(hql, StudentDetail.class);
they have different arrity (number of arguments). You mock method with one argument, and call with two. Mock, when don't know what should return, return null
.
Edit:
@Mock
List<StudentDetail> list;
//....
Mockito.when(query.getResultList()).thenReturn(list);
How you initialize and fill list? BTW, better (in my opinion) version of that code:
@Mock
SessionFactory sessionFactory;
@Mock
Query query;
@Mock
Session session;
@InjectMocks
private StudentDAO studentDAO ;
// Test get method
@Test
public void getListStudentTest() {
StudentDetail sd1 = new StudentDetail(1, "A", "X");
List<StudentDetail> list = new ArrayList<>();
list.add(sd1);
Mockito.when(query.getResultList()).thenReturn(list);
Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
Mockito.when(session.createQuery(ArgumentMatchers.anyString())).thenReturn(query);
Mockito.verify(sessionFactory.getCurrentSession());
Mockito.verify(session.createQuery(ArgumentMatchers.anyString(),ArgumentMatchers.anyObject() ));
Mockito.verify(query.getResultList());
Assert.assertEquals(sd1, studentDAO.listStudentDetail().get(0));
}
Now we mock external/framework resources ( SessionFactory
, Session
, Query
) and manually manage our data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.