【发布时间】:2017-07-05 21:25:39
【问题描述】:
我正在使用Mockito 在我当前的项目中模拟服务。
我有一个场景,我需要在代码中模拟链式方法。链式方法使用流畅的设计模式。代码如下。我找不到满足我要求的解决方案。
ProcessCall setValue = ProcessCall.url("").http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create();
我正在尝试像下面那样模拟上面的代码
@Test
public void ProcessPost(){
System.out.println("--------------------------");
ProcessCall procCall= Mockito.mock(ProcessCall.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(ProcessCall .url("").http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create()).thenReturn(??);
}
不确定在 thenReturn(??) 方法中传递什么。ProcessCall 是一个具有私有构造函数的类。它有一个 execute() 方法,我需要从调用结果中执行该方法。 我收到以下错误:
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);
Or 'a static method call on a prepared class`
For example:
@PrepareForTest( { StaticService.class })
TestClass{
public void testMethod(){
PowerMockito.mockStatic(StaticService.class);
when(StaticService.say()).thenReturn(expected);
}
}
Also, this error might show up because:
1. inside when() you don't call method on mock but on some other object.
2 . inside when() you don't call static method, but class has not been prepared.
谁能帮我解决这个问题。我被这个问题困住了,在 SO 上找不到任何合适的解决方案。
谢谢
【问题讨论】:
-
以及超越“使用 powermock 进行静态调用”;还可以考虑“不使用静态调用,因此不需要 powermock”。如果您有选择,请在此处选择选项 2;永远!
标签: java unit-testing mockito powermock