特定のクラスをテストしています。このクラスは、テストされたクラスに注入される「HttpClient」オブジェクトに渡される「GetMethod」オブジェクトを内部的にインスタンス化しています。
「HttpClient」クラスをモックしていますが、「GetMethod」クラスの 1 つのメソッドの動作も変更する必要があります。ArgumentCaptor で遊んでいますが、「when」呼び出しでインスタンス化されたオブジェクトを取得できないようです。
例:
HttpClient mockHttpClient = mock(HttpClient.class);
ArgumentCaptor<GetMethod> getMethod = ArgumentCaptor.forClass(GetMethod.class);
when(mockHttpClient.executeMethod(getMethod.capture())).thenReturn(HttpStatus.SC_OK);
when(getMethod.getValue().getResponseBodyAsStream()).thenReturn(new FileInputStream(source));
応答:
org.mockito.exceptions.base.MockitoException:
No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()