【问题标题】:Mockito MissingMethodInvocationException with my function用我的函数模拟 MissingMethodInvocationException
【发布时间】:2019-08-16 08:37:25
【问题描述】:

我正在使用 Springboot 和 Mockito 进行测试。

我想模拟一个她返回字符串的函数。

代码实现:

编辑

public String replaceContent(String url, String replace, String value) {
    return url.replace(replace, value);
}


public ResponseEntity<List<MonitorOperativeFront>> getOperativesMonitor(String userCode) {

log.info(" ---> LogInfo: start ");

String url = this.replaceContent(this.urlBase,this.stringReplace,userCode);

log.info(" ---> LogInfo: call to: " + url);
List<MonitorOperativeFront> list= null ;

MonitorOperative[] operative = this.restTemplate.getForObject(url, MonitorOperative[].class);
list.add(new MonitorOperativeFront(operative[0].getId()));
log.info(" ---> LogInfo: Success ");


return new ResponseEntity<>(list, HttpStatus.OK);

}


代码测试:

@Mock
Mockito mk;
@InjectMocks
MonitorServiceImpl monitorService;

    @Test
    public void testG() throws Exception {
        String url = "prueba/test";
        this.mk.when( monitorService.replaceContent("prueba/{id}", "{id}", "test"))
                .thenReturn(url);
    ResponseEntity<List<MonitorOperativeFront>> operative2 = monitorService.getOperativesMonitor("n11111");
    assertEquals(true,true);

错误:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

我检查了我的函数是否返回成功“prueba/test”,但在 mockito 中我得到一个错误,我不知道解决方案...

【问题讨论】:

  • 这根本不是 Mockito 的工作方式。您正在尝试模拟 Mockito 本身,并且您正在使用您的测试类,就好像它是一个模拟一样。您发布的代码确实没有意义。请阅读stackoverflow.com/a/28783849/571407了解mock的原理。

标签: java mockito


【解决方案1】:

@Mock 应该是具有您要模拟的方法的类。如果你想模拟你必须写的 replaceContent

@Mock
MonitorServiceImpl monitorServiceMock;

@InjectMocks
SomeUserOfMonitorServiceImpl monitorServiceImplUser;

@Test
public void testG() throws Exception {
   String url="http://dsgdfgdf/"
   Mockito.when( monitorServiceMock.replaceContent("prueba/{id}", "{id}", "test"))
            .thenReturn(url));
   //Do Something which calls the monitorService.replaceContent 
   monitorServiceImplUser.doSomething();

【讨论】:

  • 这段代码是正确的,但我对这段代码有问题。现在 monitorServiceMock.replaceContent 是绿色的,但是在 monitorServiceImplUser 中也使用 .replaceContent 并且我无法模拟此调用, .replaceContent 得到 null,null,null
  • 请提供monitorServiceImplUser的Code
  • monitorService.getOperativesMonitor 也使用 replaceContent,但是使用您的 Mock with monitorServiceMock 代码,我收到错误。
  • 所以你试图模拟一个在同一个类中实现的方法,就像调用模拟方法的方法一样?!
  • 可以使用间谍,但这会是代码异味。坦率地说,在另一个字符串中替换子字符串的方法根本不需要模拟。只需使用真正的实现即可。
猜你喜欢
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
相关资源
最近更新 更多