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)