Java Mockito MissingMethodInvocationMy函数异常

Java Mockito MissingMethodInvocationMy函数异常,java,mockito,Java,Mockito,我正在使用Springboot和Mockito进行测试 我想要一个mock函数,让她返回一个字符串 代码impl: 编辑 public String replaceContent(String url, String replace, String value) { return url.replace(replace, value); } public ResponseEntity<List<MonitorOperativeFront>> getOperat

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

我想要一个mock函数,让她返回一个字符串

代码impl:

编辑

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);

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

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

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

这根本不是Mockito的工作原理。您试图模拟Mockito本身,并且您正在使用您的被测类,就好像它是一个模拟。你发布的代码真的毫无意义。请阅读以理解模仿的原理。此代码是正确的,但我对此代码有一个问题。现在monitorServiceMock.replaceContent是绿色的,但在monitorServiceImplUser中也使用.replaceContent,我无法模拟此调用.replaceContent get null,null,null请提供monitorServiceImplUsermonitorService.GetOperationMonitor的代码也使用replaceContent,但使用monitorServiceMock的模拟代码,我得到一个错误。所以你试图模拟一个在同一个类中实现的方法,就像调用模拟方法的方法一样?!使用间谍是可能的,但这将是一种代码气味。坦率地说,替换另一个字符串中的子字符串的方法根本不需要模拟。只需使用真正的实现。
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.
@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();