Mockito中未调用的模拟方法 - java

您好,我有一种方法的服务:

@Service
public class CaptchaServiceImpl implements CaptchaService {

@Autowired
private MessageSource messageSource;

@Override
public boolean processCaptcha(String requestedUrl, String challenge, String userResponse) {

    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
    reCaptcha.setPrivateKey(messageSource.getMessage("reCaptcha.private.key", new Object[]{}, new Locale("pl", "PL")));
    ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(requestedUrl, challenge, userResponse);

    return reCaptchaResponse.isValid();
}

}

我为此写了一个测试:

@RunWith(MockitoJUnitRunner.class)
public class CaptchaServiceImplTest {

private CaptchaService captchaService;

@Mock
private MessageSource messageSource;

@Mock
private ReCaptchaImpl reCaptcha;

@Before
public void init() {
    captchaService = new CaptchaServiceImpl();
    ReflectionTestUtils.setField(captchaService, "messageSource", messageSource);
}

@Test
public void shouldPassReCaptchaValidation() {
    ReCaptchaTestResponse captchaResponse = new ReCaptchaTestResponse(true, "no errors");
    when(messageSource.getMessage("reCaptcha.private.key", new Object[]{}, new Locale("pl", "PL"))).thenReturn("reCaptcha.private.key");
    when(reCaptcha.checkAnswer(anyString(), anyString(), anyString())).thenReturn(captchaResponse);

    boolean reCaptchaResponse = captchaService.processCaptcha("url", "challenger", "userResponse");

    assertThat(reCaptchaResponse, is(true));
}

private class ReCaptchaTestResponse extends ReCaptchaResponse {

    protected ReCaptchaTestResponse(boolean valid, String errorMessage) {
        super(valid, errorMessage);
    }
}

}

ReCaptchaResponse是受保护的类...

因此,当我运行测试时,我得到:

 java.lang.AssertionError: 
 Expected: is <true>
 got: <false>

由于某种原因,我的模拟方法checkAnswer永远不会被调用,并且我的captchaResponse对象也不会返回,并且我已经没有了为什么的想法。有人可以告诉我为什么会这样吗?也许我缺少一些东西:/

更新:

所以我更新了我的CaptchaService:

@Autowired
private ReCaptchaImpl reCaptcha;

@Override
public boolean processCaptcha(String requestedUrl, String challenge, String userResponse) {
    reCaptcha.setPrivateKey(messageSource.getMessage("reCaptcha.private.key", new Object[]{}, new Locale("pl", "PL")));
    ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(requestedUrl, challenge, userResponse);

    return reCaptchaResponse.isValid();
}

现在测试是绿色的! :) 谢谢

java大神给出的解决方案

这就是问题:

ReCaptchaImpl reCaptcha = new ReCaptchaImpl();

那只是创建一个新实例-完全没有使用您的模拟。注意您如何不将模拟传递给任何东西-您希望生产代码如何使用它?

嘲讽对于注入的依赖项甚至是工厂返回的依赖项都有好处,您可以使工厂为您返回模拟-但您只是在调用构造函数。

您可以为此使用PowerMock,但是我建议重新设计以避免完全不需要模拟,或者允许将依赖项注入到某个地方。

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java.net.URI.create异常 - java

java.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…

从方法返回数组-Java - java

private static Coordinate[] getCircleCoordintaes() { Coordinate coordinates[] = {new Coordinate(0, 0)}; return coordinates; } 以上程序工作正常。在上面的程序中,返回的坐标数组首先初始化了数组使用这条线Coordinate coordi…