javaspringtestingmockitostubbing

mockito.exceptions.misusing.PotentialStubbingProblem


I try to mock restTemplate and test this method

    public HashMap<String, String> getAvailableCurrencies() {
       SymbolEndpointResponce symbolResponce = restTemplate.getForObject(this.baseUrl, SymbolEndpointResponce.class);

      System.out.println(symbolResponce);

      if(symbolResponce.getSuccess()) {
        return symbolResponce.getSymbols();
      }

      return null;
     }

Tets class, so try to prepare a sample responce and use it:

@Test
void getAvailableCurrencies() throws URISyntaxException {

    Mockito.doReturn(getMockSymbols()).when(restTemplate)
            .getForObject(null, SymbolEndpointResponce.class);
    HashMap<String, String> symbols = service.getAvailableCurrencies();
    assertEquals(4, symbols.size());
}

private SymbolEndpointResponce getMockSymbols() {
    ...
}

And I am getting an error

    org.mockito.exceptions.misusing.PotentialStubbingProblem: 
   Strict stubbing argument mismatch. Please check:
 - this invocation of 'getForObject' method:
    restTemplate.getForObject(
    null,
    class de.c24.finacc.klt.model.SymbolEndpointResponce
);
    -> at de.c24.finacc.klt.services.SymbolService.getAvailableCurrencies(SymbolService.java:35)
 - has following stubbing(s) with different arguments:
    1. restTemplate.getForObject(
    null,
    class de.c24.finacc.klt.model.SymbolEndpointResponce
);
      -> at de.c24.finacc.klt.services.SymbolServiceTest.getAvailableCurrencies(SymbolServiceTest.java:37)
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
However, there are legit scenarios when this exception generates false negative signal:
  - stubbing the same method multiple times using 'given().will()' or 'when().then()' API
    Please use 'will().given()' or 'doReturn().when()' API for stubbing.
  - stubbed method is intentionally invoked with different arguments by code under test
    Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).
For more information see javadoc for PotentialStubbingProblem class.
    

Cannot figure out what's going wrong when the arguments are identical.


Solution

  • Use empty string instead of null (it was your own answer :) )