java - org.mockito.exceptions.misusing.MissingMethodInitationException

标签 java junit mockito

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 制作为模拟,但仍然遇到相同的错误

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

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.

最佳答案

您似乎正在尝试在调用静态方法时使用 when,即 not supported by Mockito 。正如错误消息所示,参数必须是“模拟上的方法调用”,并且我在您的代码中看不到任何表明 RequestBuilder 是模拟的内容。

关于java - org.mockito.exceptions.misusing.MissingMethodInitationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37524356/

相关文章:

java - 如果 WebAppContext 无法启动,如何取消启动或关闭 jetty

java - 与 CompletableFuture 一起使用的异常处理方法

java - 如何模拟方法内的对象方法调用?

java - 如何使用模拟对象替换内部代码部分中的原始对象?

java - Mockito 如何监视模拟内部方法调用?

java - android反射方法调用——什么是接收者

java - xPath 适用于最后一页,但不适用于第一页 - Selenium Java

java - 具有异常预期的 JUnit 测试(多个断言)

java - 使用 Clock.fixed() 模拟 LocalDate.now(clock)

java - Mockito:如何替换被测类调用的类的方法?