如何使用power mock模拟将List类型作为参数的私有方法?

时间:2017-09-25 11:46:56

标签: java unit-testing junit mocking powermock

大家好我正在尝试模拟一个将List类型作为参数的私有方法代码细节的一部分如下:

CustomerVerification Class

    public class CustomerVerification{

creditCheck.setSuffix(null);
        String pinAndPreciseId = null;
        try {
            pinAndPreciseId = executeCreditCheck(creditCheck, errorResponses, transactionId);
            if (pinAndPreciseId.contains("Error")) {
                ErrorResponse errorResponse = new ErrorResponse("EXPERIAN", pinAndPreciseId, "E01", transactionId);
                errorResponses.add(errorResponse);
                customerVerification.setErrorResponses(errorResponses);
                return customerVerification;
            }
        } catch (Exception e) {
            throw new EnterpriseCustomerVerificationException(e.getMessage());
        } 

        }

executeCriditCheck类

private String executeCreditCheck(CreditCheck creditCheck, List<ErrorResponse> errorResponses, String transactionId)
            throws UnsupportedOperationException, IOException {
        LOG.info("Calling experian");
        String pinAndPreciseId = null;
        String request = null;
        String referenceId = null;
        DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date today = new Date();
        referenceId = formatter.format(today);

        HttpPost experianHttpPost = getExperianHttpPostMethod();
        NetConnectRequest netConnectRequest = ExperianCreditMapper.map(creditCheck, eai, dbHost, referenceId,
                experianProduct, opInitials, preamble, subCode, arfVersion, venderNumber, vendorVersion, null,
                CustomRRDashKeyword);
        System.err.println("REQUEST -- " + netConnectRequest.toString());
        request = "NETCONNECT_TRANSACTION=" + URLEncoder.encode(netConnectRequest.toString());

        experianHttpPost.setEntity(new ByteArrayEntity(request.getBytes("UTF-8")));
        HttpResponse response = experianHttpClient.execute(experianHttpPost);
        pinAndPreciseId = ExperianCreditMapper.getPIN(response);
        return pinAndPreciseId;
    }

有人可以给我一个关于如何模拟私有方法executeCreditCheck 的想法,其中有3个参数,其中一个是List类型。

  

注意:我这里只提供了部分代码。请一些人给   关于如何模拟 executeCreditCheck 方法的想法。

1 个答案:

答案 0 :(得分:1)

关于测试私人方法或如何测试的天气存在巨大的争论。就个人而言,我不直接测试私人方法。会有公共方法调用私有方法,我更喜欢间接测试,我可能做错了。

回到你的问题,

您可以使用powermock Whitebox.invokeMethod()来测试私有方法。它的语法是

1027