为什么模拟私有方法进入方法?

Why mocking a private method enters method?

我在测试中使用 PowerMockito 模拟私有方法。

validator = spy(new CommentValidator(form, request));
PowerMockito.when(
        validator,
        method(CommentValidator.class, "isCaptchaValid",
        HttpServletRequest.class))
    .withArguments(Mockito.any())
    .thenReturn(true);

当我 运行 测试时,我在 isCaptchaValid 方法的第二行得到 java.lang.reflect.InvocationTargetExceptionNullPointerException,如下所示:

private boolean isCaptchaValid(HttpServletRequest request) {
    Captcha captcha =
        (Captcha) request.getSession().getAttribute("attribute");
    if (captcha == null) {
        log.debug(String.format("O valor do captcha da sessão esta nulo. IP: [%s]",
            IPUtil.getReaderIp(request)));
        return false;
    }
    if (captcha.isInputValid(
            request.getParameter("captcha").toString().toUpperCase())) {
        return true;
    }
    return false;
}

public final Boolean isInputValid(String pInput) {

    if (getPuzzle() == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("puzzle is null and invalid. Will return Boolean.FALSE");
        }
        return Boolean.FALSE;
    }

    Boolean returnValue = verifyInput(pInput);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Validation of puzzle: " + returnValue);
    }
    disposePuzzle();

    return returnValue;
}

如果我在模拟它的行为,为什么要考虑该方法的实现?有没有办法避免这种情况?我需要模拟它的原因是因为我无法提供 Captcha 对象。

问题已解决

通过调用

PowerMockito.when(
        validator,
        method(CommentValidator.class, "isCaptchaValid",
        HttpServletRequest.class))
    .withArguments(Mockito.any())
    .thenReturn(true);

首先,方法本身由 PowerMockito 验证,因此 NPE 很可能被发现。

为了避免这种情况,您需要反转该逻辑。

doReturn(true).when(validator, "isCaptchaValid", any(HttpServletRequest.class));

这使得 PowerMockito 忽略方法的主体并立即 return 你想要的。