简体   繁体   中英

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. Is there a way to unit test it, if not maybe just make jacoco/sonarqube ignore it?

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));
        };
    }

You can find the coverage here

I would extract a method with Lambda expression contents, and then convert lambda itself to class field. That way you will be able to test extracted method and lambda itself will not be included in coverage. I would also make the field static to avoid unnecessary instantiation of functional interface implementation.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM