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.
答案 0 :(得分:1)
您似乎在尝试使用when
来调用静态方法not supported by Mockito。正如错误消息所说,参数必须是“模拟上的方法调用”,我在代码中看不到任何暗示RequestBuilder
是模拟的内容。
答案 1 :(得分:0)
使用Powermockito概念我们可以模拟静态方法
此链接帮助我解决了我的问题
https://examples.javacodegeeks.com/core-java/mockito/powermock-mockito-integration-example/