I am using Springboot with Mockito for test.
I want mock a function that she return a string.
Code impl:
EDIT
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);
}
Code Test:
@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);
Err:
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.
I checked that my function return success "prueba/test" but in mockito I get a error and I don't know resolve...