私の現在のプロジェクトでサービスをモックするためにMockito
を使用しています。 私は、コード内で連鎖したメソッドをモックする必要があるシナリオを持っています。連鎖法では流暢なデザインパターンを使用します。コードは以下の通りです。私は私の要求を満たす解決策を見つけることができませんでした。Java/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に合格する(??)method.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に進みます。そして、いつも!ありがとう。 – GhostCat