0

I am new to the Mockito. I am trying to write test case for following restcontroller method.

    @ResponseBody   
    @RequestMapping(value = Constants.PATH_NAME, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseData abc(@RequestBody RequestData requestData, HttpServletRequest request)
            throws Exception
        request.setAttribute("req", requestData);
        if (Validation.numberValiadtion(requestData.getNumber())) {  // where Validation is the static  class of external jar
            LOGGER.info("Number validated successfully--");
        } else {
            String errorMessage = Constants.INVALID_NUMBER;
            throw new NumberNotFoundException(errorMessage);
        }
        ResponseData reponseData = service.getScheduleData(requestData); //service is autowired

        return reponseData;
    }

and my testcase is

 @RunWith(MockitoJUnitRunner.Silent.class)

 public class ApplicationTest {

 @Mock
 AbcService service;

  @Mock
  HttpServletRequest httpReq;

 @Test
  public void abcSuccess() throws Exception {

  SampleController restController = Mockito.mock(SampleController.class);
  Validation validation = Mockito.mock(Validation.class);

 RequestData req = new RequestData();
 ResponseData respData = new ResponseData();
 Mockito.when(restController.abc(req, httpReq)).thenReturn(respData); 
Mockito.when(validation.numberValiadtion("467")).thenReturn(true); --err line

 Mockito.when(service.getData(req)).thenReturn(respData);

  }
}

And the errortrace-

  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.

Could you please help me out to solve the issue.Validation calss is the static class (in external Jar)

madhuri
  • 103
  • 2
  • 11
  • Is `service` a mock ? – R.G May 09 '20 at 13:53
  • Yes, I mentioned @mock . Getting exception with validation class. – madhuri May 09 '20 at 14:06
  • It shouldn't . Could you update the question with the entire test class code or alteast a minimum reproducible example ? – R.G May 09 '20 at 14:07
  • I have edited code. Please help. – madhuri May 10 '20 at 09:46
  • If `Validation` is a framework class , please share the path else share the `Validation` class code – R.G May 10 '20 at 09:50
  • No. It's normal static class where we have written numbers validation with some rules. – madhuri May 10 '20 at 14:15
  • Please read through this [Q&A](https://stackoverflow.com/q/4482315/4214241) to understand why static methods cannot be mocked. Please check if there is a real need to mock `validation.numberValiadtion()` and I think the test will work as expected without that mock. – R.G May 11 '20 at 01:54
  • do I need to use real implementation? – madhuri May 11 '20 at 04:54
  • If my understanding is correct , it is just a helper method to validate a number. Have you realized that the main issue with this test is that , you are mocking the class under test itself , which fails the whole purpose of this unit test. – R.G May 11 '20 at 04:58

0 Answers0