简体   繁体   English

如何对包含使用谓词的 lambda function 的 function 进行单元测试

[英]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));
        };
    }

You can find the coverage here你可以在这里找到报道

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.

相关问题 如何将 java.util.function.Predicate 实现为 Kotlin lambda? - How to implement a java.util.function.Predicate as Kotlin lambda? 用 spock 测试 lambda function - Test lambda function with spock A function 未在单元测试中执行 - A function is not executed in an unit test 如何为 Azure Function TimerTrigger 实现单元测试 - How to implement unit test for Azure Function TimerTrigger 如何更正我的GCD功能单元测试 - How to correct my unit test for GCD function 当一个函数调用另一个返回布尔值的函数时,如何进行单元测试? - How to unit test when a function calls another function returning a boolean? Mockito - 如果参数设置在另一个 function 调用的 function 中,如何进行单元测试 - Mockito - how to unit test if argument is set in a function called by another function 如何对使用JNI的库进行单元测试? - How to unit test a library that uses JNI? 如何在 Java 函数中适当地组合 Predicate 和 Function? - How to suitably compose Predicate and Function in a Java function? 如何为使用 AndroidSchedulers.mainThread() 的函数编写测试; 来自 RxAndroid - How to write a test for a function that uses AndroidSchedulers.mainThread(); from RxAndroid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM