http 호출을 PowerMockito를 사용하여 모의하려고하는데, 기능에 문제가 있습니다. 내 계획은 경로에 특정 문자열이 들어 있는지 확인한 다음 mock 객체를 반환하는 것입니다.powermock에() 작동하지 않습니다.
Response res = client.target(theMovieDbURL)
.path("/3/genre/movie/list")
.queryParam("api_key", apiKey)
.request()
.buildGet()
.invoke();
내가 contains()
에서 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 호출을 만들려고 노력 오전 nullpointer를 얻을 수 있습니다 : 그래서 나는 다음과 같은 기능을 가지고 그것은 매력처럼 작동하지만 다른 경로에 대해 다른 응답을해야하므로 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
지금 당장 가지고있는 유일한 생각은 잘못된 것을 포함 할 수 있습니다. 정적 가져 오기 섹션을 추가 할 수 있습니까? –