我有这样一个查询:
final String namedQueryName = Report.QUERY_NAME_FIND_BY_ENTITY;
final Query query = getEntityManager().createNamedQuery(namedQueryName);
query.setParameter("pEntityName", entityName);
这很好用,但是,作为一种好习惯,我想使用TypedQuery,所以我将其更改为:
final String namedQueryName = Report.QUERY_NAME_FIND_BY_ENTITY;
final TypedQuery<Report> query = getEntityManager().createNamedQuery(namedQueryName, Report.class);
query.setParameter("pEntityName", entityName);
现在,当我使用mockito getEntityManager()运行junit测试时,返回null。
@Mock
private static TypedQuery<Report> query;
@Test
public void findByEntity_empty() {
when(entityManager.createNamedQuery(Report.QUERY_NAME_FIND_BY_ENTITY)).thenReturn(query);
when(query.setParameter("pEntityName", "NOTHING")).thenReturn(query);
List<Report> reportsDB = new ArrayList<>();
when(query.getResultList()).thenReturn(reportsDB);
assertEmpty(reportDAO.findByEntity("NOTHING"));
}
为什么getEntityManager().createNamedQuery(namedQueryName, Report.class);
返回null而不是创建查询?
你有什么主意吗?