-1

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...

Eduardo
  • 47
  • 1
  • 7
  • 3
    That's not, at all, how Mockito works. You're trying to mock Mockito itself, and you're using your class under test as if it was a mock. The code you posted really makes no sense. Please read https://stackoverflow.com/a/28783849/571407 to understand the principle of mocking. – JB Nizet Aug 16 '19 at 08:41

1 Answers1

3

The @Mock should be the class which has the Method you would like to mock. If you would like to mock the replaceContent you have to write

@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();
Hagen
  • 66
  • 2
  • This code is correct, but I have a problem with this code. Now monitorServiceMock.replaceContent is green, but in monitorServiceImplUser also use .replaceContent and I can't mock this call, .replaceContent get null,null,null – Eduardo Aug 16 '19 at 09:18
  • Please provide the Code of monitorServiceImplUser – Hagen Aug 16 '19 at 09:21
  • monitorService.getOperativesMonitor also use replaceContent, but with your code for Mock with monitorServiceMock, I get error. – Eduardo Aug 16 '19 at 09:26
  • So you are trying to Mock one method which is implemented in the same class like the method which is calling the mocked method?! – Hagen Aug 16 '19 at 09:33
  • yes, because replaceContent needs 3 parameters, @Value("${urlRest.urlBase}") private String urlBase; etc... – Eduardo Aug 16 '19 at 09:36
  • You should move the method to a different class and mock this new class. I believe that it is not possible to mock it, if it is in the same class. – Hagen Aug 16 '19 at 09:40
  • 1
    It would be possible using a spy, but this would be a code smell. Frankly, a method replacing a substring in another string doesn't need to be mocked at all. Just use the real implementation. – JB Nizet Aug 16 '19 at 09:42