2017-10-21 4 views
0

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' 
+0

시험 준비 부분을 준비하십시오. 공유 한 코드에는'mockResponse'에 대한 호출이 없습니다. –

+0

@AlehMaksimovich 지금 거기에 있습니다 :) – munHunger

+0

지금 당장 가지고있는 유일한 생각은 잘못된 것을 포함 할 수 있습니다. 정적 가져 오기 섹션을 추가 할 수 있습니까? –

답변

0

더 : 여기에 잘못된 토끼 구멍 아래로 가고있다.

것은 : 테스트의 목적은 특정 생산 코드 테스트가 포함하도록되어 그 요구 사항을 구현하도록하는 것입니다. 다른 건 없어.

주요 내용 : 여러 가지 측면을 처리 할 수있는 코드 하나에 대해 여러 가지 간단한 테스트를 선호합니다.

의미 : 당신이 가서() 볼 수 있습니다에 포함 된 내용을 여러 가지 다른 가능성이 곳

when(webTarget.path(contains(pathContains))).thenReturn(webTarget); 

사용을 시작하지 않습니다. 어떻게 든 당신의 테스트 코드 내에서 영리 "비즈니스 로직을"표현하고자하는 대신, 당신은 오히려 명시 적으로 즉

when(webTarget.path(contains(A))).thenReturn(webTargetA); 

...

when(webTarget.path(contains(B))).thenReturn(webTargetB); 

말 n 개의 테스트가 있습니다. 당신은 그것을 "현명하게"만들고 싶습니다. 그러지 마. 대신, 똑바로 특정 설정을 만드는 테스트 코드를 작성에 중점을 두어 하나의 것을 테스트합니다.

아직 즉

: 매개 변수 특정 반환 값을 제공하기 위해 테스트 코드에서 어떤 필요가 있어야한다. 은 프로덕션 코드에서 볼 수있는 설정 (및 그에 따른 경로)을 정의합니다. 하나의 테스트에서는 경로 A를 사용하고 다른 테스트에서는 경로 B를 사용하도록 지시합니다. 그리고 정교한 당신의 욕망은 얇은 공기 속에서/그때의 논리가 사라집니다.