org.mockito.exceptions.misusing.MissingMethodInvocationException

问题描述 投票:0回答:2

Junit测试代码:

    @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");
    }

我将RequestBuilder设为mock但仍然遇到同样的错误

当我运行上面的代码时,它首先在()时给出以下错误。

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.
java junit mockito
2个回答
1
投票

看起来你试图在调用静态方法时使用when,这是not supported by Mockito。正如错误消息所说,参数必须是“模拟上的方法调用”,我在代码中看不到任何暗示RequestBuilder是模拟的东西。


0
投票

使用Powermockito概念,我们可以模拟静态方法

这个链接帮助我解决了我的问题

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

© www.soinside.com 2019 - 2024. All rights reserved.