[英]How to unit test a function that contains a lambda function that uses Predicate
I've been trying to unit test this function for days, but can't seem to work it out, and I have similar functions in all the services which really lowers the code coverage.几天来我一直在尝试对这个 function 进行单元测试,但似乎无法解决,而且我在所有服务中都有类似的功能,这确实降低了代码覆盖率。 Is there a way to unit test it, if not maybe just make jacoco/sonarqube ignore it?
有没有办法对它进行单元测试,如果没有,也许只是让 jacoco/sonarqube 忽略它?
public Specification<ContratEntity> getSpecification(ContratDto dto) {
List<DepartementEntity> departementEntities = securityService.checkDepartementFiltre();
String[] filtreBus = securityService.checkBuFiltre();
List<TypeContratEnum> typeContratEnums = securityService.checkTypeContratFiltre();
return (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (dto.getNumero() != null && !"".equals(dto.getNumero())) {
predicates.add(cb.like(cb.lower(root.get("numero")), "%" + dto.getNumero().toLowerCase() + "%"));
}
if (dto.getBu() != null && !"".equals(dto.getBu())) {
predicates.add(cb.like(cb.lower(root.get("bu")), "%" + dto.getBu().toLowerCase() + "%"));
}
if (dto.getDepartement() != null && dto.getDepartement().getAbreviation() != null) {
predicates.add(cb.like(cb.lower(root.get("departement").get("abreviation")), dto.getDepartement().getAbreviation().toLowerCase()));
}
if (departementEntities != null) {
predicates.add(cb.or(
root.get("departement").in(departementEntities),
root.get("departement").isNull()
));
}
if (filtreBus != null) {
predicates.add(root.get("bu").in(filtreBus));
}
if (typeContratEnums != null) {
predicates.add(root.get("type").in(typeContratEnums));
}
if (dto.getLbvFournisseur() != null && dto.getLbvFournisseur().getCnuf_frn() != null && !dto.getLbvFournisseur().getCnuf_frn().equals("")) {
predicates.add(cb.or(
cb.like(cb.lower(root.join("lbvFournisseur", JoinType.LEFT).get("cnuf_frn")), dto.getLbvFournisseur().getCnuf_frn().toLowerCase()),
cb.like(cb.lower(root.join("hlvFournisseur", JoinType.LEFT).get("cnuf_frn")), dto.getLbvFournisseur().getCnuf_frn().toLowerCase()),
cb.like(cb.lower(root.join("mlvFournisseur", JoinType.LEFT).get("cnuf_frn")), dto.getLbvFournisseur().getCnuf_frn().toLowerCase())
));
}
if (dto.getLbvFournisseur() != null && dto.getLbvFournisseur().getRaison_sociale() != null) {
predicates.add(cb.or(
cb.like(cb.lower(root.join("lbvFournisseur", JoinType.LEFT).get("raison_sociale")), "%"+ dto.getLbvFournisseur().getRaison_sociale().toLowerCase() + "%"),
cb.like(cb.lower(root.join("hlvFournisseur", JoinType.LEFT).get("raison_sociale")), "%"+ dto.getLbvFournisseur().getRaison_sociale().toLowerCase() + "%"),
cb.like(cb.lower(root.join("mlvFournisseur", JoinType.LEFT).get("raison_sociale")), "%"+ dto.getLbvFournisseur().getRaison_sociale().toLowerCase() + "%")
));
}
if (dto.getLbvFournisseur() != null && dto.getLbvFournisseur().getIce() != null && dto.getLbvFournisseur().getIce() != "") {
predicates.add(cb.or(
cb.like(cb.lower(root.join("lbvFournisseur", JoinType.LEFT).get("ice")), dto.getLbvFournisseur().getIce().toLowerCase()),
cb.like(cb.lower(root.join("hlvFournisseur", JoinType.LEFT).get("ice")), dto.getLbvFournisseur().getIce().toLowerCase()),
cb.like(cb.lower(root.join("mlvFournisseur", JoinType.LEFT).get("ice")), dto.getLbvFournisseur().getIce().toLowerCase())
));
}
if (dto.getType() != null) {
predicates.add(cb.equal(root.get("type"), dto.getType()));
}
Predicate[] predicatesArray = new Predicate[predicates.size()];
query.orderBy(cb.desc(root.get("id")));
return cb.and(predicates.toArray(predicatesArray));
};
}
I would extract a method with Lambda expression contents, and then convert lambda itself to class field.我会提取一个带有 Lambda 表达式内容的方法,然后将 lambda 本身转换为 class 字段。 That way you will be able to test extracted method and lambda itself will not be included in coverage.
这样您就可以测试提取的方法,而 lambda 本身将不会包含在覆盖范围内。 I would also make the field static to avoid unnecessary instantiation of functional interface implementation.
我还将创建字段 static 以避免不必要的功能接口实现实例化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.