我正在尝试测试 JPA 存储库。这是我的客户/测试代码:
@Test
public void testFindBoitesByMultiFieldWithIdentifiantSet() {
BoiteDataOnDemand dod = new BoiteDataOnDemand();
Boite boite = dod.getSpecificBoite(0);
boite.setIdentifiant("theIdentifiant");
boiteRepository.save(boite);
assertEquals(10, Boite.countBoites());
BoiteQueryInfo boiteQueryInfo = new BoiteQueryInfo();
boiteQueryInfo.setIdentifiant("theIdentifiant");
List<Boite> boites = boiteRepository.findBoitesByMultiField(boiteQueryInfo, 1, 5, "identifiant", "desc");
assertEquals(1, boites.size());
}
这是存储库方法:
@Override
public List<Boite> findBoitesByMultiField(BoiteQueryInfo boiteQueryInfo, Integer firstResult_, Integer maxResults_, String sort_, String order_) {
log.debug("findBoitesByMultiField");
final String identifiant = boiteQueryInfo.getIdentifiant();
final Date dateOuvertureFrom = boiteQueryInfo.getDateOuvertureFrom();
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Boite> c = criteriaBuilder.createQuery(Boite.class);
Root<Boite> boite = c.from(Boite.class);
List<Predicate> criteria = new ArrayList<Predicate>();
...
if (identifiant != null && !identifiant.trim().equals("")) {
ParameterExpression<String> parameter = criteriaBuilder.parameter(String.class, "identifiant");
Predicate condition = criteriaBuilder.like(boite.<String> get("identifiant"), parameter);
criteria.add(condition);
}
if (dateOuvertureFrom != null && dateOuvertureTo != null) {
ParameterExpression<Date> parameterDateOuvertureFrom = criteriaBuilder.parameter(Date.class, "dateOuvertureFrom");
ParameterExpression<Date> parameterDateOuvertureTo = criteriaBuilder.parameter(Date.class, "dateOuvertureTo");
Predicate condition = criteriaBuilder.between(boite.<Date> get("dateOuverture"), parameterDateOuvertureFrom, parameterDateOuvertureTo);
criteria.add(condition);
} else if (dateOuvertureFrom != null) {
ParameterExpression<Date> parameter = criteriaBuilder.parameter(Date.class, "dateOuvertureFrom");
Predicate condition = criteriaBuilder.greaterThanOrEqualTo(boite.<Date> get("dateOuverture"), parameter);
criteria.add(condition);
} else if (dateOuvertureTo != null) {
ParameterExpression<Date> parameter = criteriaBuilder.parameter(Date.class, "dateOuvertureTo");
Predicate condition = criteriaBuilder.lessThanOrEqualTo(boite.<Date> get("dateOuverture"), parameter);
criteria.add(condition);
}
...
if (order.equalsIgnoreCase("desc")) {
c.orderBy(criteriaBuilder.desc(boite.get(sort)));
} else {
c.orderBy(criteriaBuilder.asc(boite.get(sort)));
}
for (Predicate predicate : criteria) {
c.where(criteriaBuilder.and(predicate));
}
TypedQuery<Boite> q = em.createQuery(c);
if (identifiant != null && !identifiant.trim().equals("")) {
q.setParameter("identifiant", "%" + identifiant + "%");
}
if (dateOuvertureFrom != null && dateOuvertureTo != null) {
q.setParameter("dateOuvertureFrom", dateOuvertureFrom);
q.setParameter("dateOuvertureTo", dateOuvertureTo);
} else if (dateOuvertureFrom != null) {
q.setParameter("dateOuvertureFrom", dateOuvertureFrom);
} else if (dateOuvertureTo != null) {
q.setParameter("dateOuvertureTo", dateOuvertureTo);
}
...
return q.setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}
但是,测试总是在assertEquals(1, boites.size());
指示没有返回结果时失败,即 ( java.lang.AssertionError: expected:<1> but was:<0>
)。
我强烈怀疑这里有问题:
for (Predicate predicate : criteria) {
c.where(criteriaBuilder.and(predicate));
}
但我不确定如何“和”标准。
任何人都可以提供建议吗?
PS仅供参考,BoiteDataOnDemand
将随机10行插入boite
表中。
编辑:对代码进行了编辑以使其更短。