简体   繁体   English

Mockito中未调用的模拟方法

[英]Mocked method not called in Mockito

Hello I have one service with a method: 您好我有一个方法的服务:

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

} }

and I wrote a test for it: 我为它写了一个测试:

@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 is protected class... ReCaptchaResponse是受保护的类......

So when I run my test I am getting: 因此,当我运行测试时,我得到:

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

For some reason my mocked method checkAnswer is never called and my captchaResponse object is never returned and I have ran out of ideas why. 由于某种原因,我的模拟方法checkAnswer永远不会被调用,我的captchaResponse对象永远不会被返回,我已经没有想法了。 Could someone tell my why it is happening? 有人能告诉我为什么会这样吗? Maybe I am missing something :/ 也许我错过了一些东西:/

UPDATE: 更新:

So I have updated my CaptchaService: 所以我更新了我的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();
}

and now test is green! 现在测试是绿色的! :) Thanks :) 谢谢

This is the problem: 这就是问题:

ReCaptchaImpl reCaptcha = new ReCaptchaImpl();

That's just creating a new instance - your mock isn't being used at all. 那只是创建一个新实例 - 你的模拟根本就没用过。 Note how you're not passing your mock into anything - how did you expect the production code to use it? 注意您如何不将模拟传递给任何东西-您希望生产代码如何使用它?

Mocks are good for injected dependencies, or even dependencies which are returned by a factory where you can make the factory return the mock for you - but you're just calling the constructor. 模拟很适合注入依赖项,甚至是工厂返回的依赖项,你可以让工厂为你返回模拟 - 但你只是调用构造函数。

You could use PowerMock for this, but I'd suggest redesigning either to avoid needing the mock at all, or to allow the dependency to be injected somewhere. 可以使用PowerMock ,但我建议重新设计以避免需要模拟,或者允许在某处注入依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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