私はPowerMockitoを使用してhttp呼び出しをモックしようとしていますが、機能に問題があります。 パスに特定の文字列が含まれているかどうかを確認してから、モックオブジェクトを返します。powermockが動作していません
Response res = client.target(theMovieDbURL)
.path("/3/genre/movie/list")
.queryParam("api_key", apiKey)
.request()
.buildGet()
.invoke();
私はからanyString()
にモックを変更する場合:
import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
import static org.mockito.Matchers.*;
import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.powermock.api.mockito.PowerMockito.*;
private static <T> void mockResponse(Class<T> type, T response, String pathContains) throws Exception
{
mockStatic(ClientBuilder.class);
Client client = mock(Client.class);
when(ClientBuilder.class, "newClient").thenReturn(client);
WebTarget webTarget = mock(WebTarget.class);
when(client.target(anyString())).thenReturn(webTarget);
//This is what doesn't work
when(webTarget.path(contains(pathContains))).thenReturn(webTarget);
when(webTarget.queryParam(any(), any())).thenReturn(webTarget);
Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
when(webTarget.request()).thenReturn(invocationBuilder);
Invocation invocation = mock(Invocation.class);
when(invocationBuilder.buildGet()).thenReturn(invocation);
Response res = mock(Response.class);
when(invocation.invoke()).thenReturn(res);
when(res.readEntity(type)).thenReturn(response);
}
mockResponse(GenreList.class, new GenreList(new Genre(0, "g")), "genre");
問題は私がHTTP呼び出しを行うにしようとしていますnullポインタを取得することです: は、だから私は、次の機能を持っていますそれは魅力のように機能しますが、私は異なるパスに対して異なる応答をする必要があるので、私はanyString()
のままにすることはできません。私もeq()
に変更しようとしましたが、どちらもうまくいきません。
私はここで何が間違っていますか?私のGradleから
:非答えの
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.6.6'
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.6.6'
testCompile group: 'org.powermock', name: 'powermock-module-junit4-rule', version: '1.6.6'
テストの模擬準備の部分を入力してください。あなたが共有しているコードに 'mockResponse'を呼び出すことはありません。 –
@AlehMaksimovichそれは今存在しています:) – munHunger
私はあなたが間違った 'を含む'を使用している可能性があるという唯一のアイデアです。静的インポートセクションを追加できますか? –