0

Junit Test Code :

    @Test
    public void validscheduleRecordingPriority() throws Exception{
        //check the code        

        RequestBuilder requestBuilder = mock(RequestBuilder.class);
        RecordingSchedulesPriorityResponse prioritySchedules = new RecordingSchedulesPriorityResponse();
        List<BigInteger> list = new ArrayList<BigInteger>();
        AMSRequest request = new AMSRequest();
        when(RequestBuilder.buildSeriesPriorityRequest(DEVICE_ID, list)).thenReturn(request); //here is the error
        AMSResponse response  = new AMSResponse();
        Result result = new Result();
        result.setStatusCode(0);
        List<Result> listResult = new ArrayList<Result>();
        listResult.add(result);
        response.setResult(listResult);
        when(AMSClient.postAMS("http://localhost:8080/ams/DVR", request)).thenReturn(response);
        ScheduleRecordingResponse response2 = dvrService.scheduleRecordingPriority(DEVICE_ID, prioritySchedules);
        assertEquals(response2.getDescription(),"Update Series Priority successful");
    }

I made RequestBuilder as mock but still getting the same error

when I run the above code, it is giving the following error @ first when().

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.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
Sat
  • 3,520
  • 9
  • 39
  • 66
  • RequestBuilder is not a mock object – Jens May 30 '16 at 11:22
  • Can't we mock the RequestBuilder? – Sat May 30 '16 at 11:23
  • As Adrian points out: you can't *mock* a static method call with Mokito. You *could* use PowerMock and its keen (which allow for mocking static methods); but I strongly recommend to not do so. If your design turns out to be not testable (and *static* methods very often push you into the not-testable corner) ... then rework your design to make it testable. – GhostCat May 30 '16 at 11:42

2 Answers2

1

It looks like you're attempting to use when on a call to a static method, which is not supported by Mockito. As the error message says, the argument has to be "a method call on a mock", and I can see nothing in your code that suggests that RequestBuilder is a mock.

Community
  • 1
  • 1
Adrian Cox
  • 6,204
  • 5
  • 41
  • 68
0

Using Powermockito concept we can mock static methods

this link helped me to solve my problem

https://examples.javacodegeeks.com/core-java/mockito/powermock-mockito-integration-example/

Sat
  • 3,520
  • 9
  • 39
  • 66