Mockito:始终使用Mockito从SQLQuery返回零结果

时间:2018-10-23 11:45:29

标签: mockito junit4

我已经为一个方法创建了一个模拟测试,但是它总是返回空。我检查了数据库中是否有给定条件(电子邮件和batchId)的数据。我要去哪里错了?

@Test
public void  testGetStudentResultByNameAndId() {
    String name = "fgafsg@gmail.com";
    Number batchId = 23;

    Session session = Mockito.mock(Session.class);
    SQLQuery sqlQuery = Mockito.mock(SQLQuery.class);
    Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
    Mockito.when(session.createQuery(" select id , tracking_type_id , date_created from STUDENT_RESULT_INFO "+
             " where name = '" + name + "' and  batch_id = "+batchId  +  " and tracking_type_id in (1,2) ")).thenReturn(sqlQuery);
    Mockito.when(sqlQuery.addScalar("id", StringType.INSTANCE)).thenReturn(sqlQuery);
    Mockito.when(sqlQuery.addScalar("tracking_type_id", StringType.INSTANCE)).thenReturn(sqlQuery);
    Mockito.when(sqlQuery.addScalar("date_created", TimestampType.INSTANCE)).thenReturn(sqlQuery);
    Mockito.when(sqlQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)).thenReturn(sqlQuery);
    Mockito.when(sqlQuery.list()).thenReturn(new ArrayList<Object>());
    System.out.println("sqlQuery.list() :"+sqlQuery.list());
}

我的输出:

query.list() :[]

0 个答案:

没有答案