2015-01-07 1 views
0

PowerMockito 스파이를 사용하여 일부 코드를 테스트하려고하는데 테스터에 구성된 값을 반환하기 위해 메서드를 스텁 (getRootTagMap - 아래 참조)합니다 (메서드가 private이기 때문에 PowerMockito 사용).PowerMockito 스파이가 값을 반환한다고 말하면서 실제 메소드를 호출하는 이유는 무엇입니까?

그러나 값을 반환하는 대신 생성 된 값을 반환하지 않고 항상 실제 메서드를 호출합니다. 내가 뭐하는 거지

확실하지 잘못된

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

import static org.powermock.api.mockito.PowerMockito.spy; 


@RunWith(PowerMockRunner.class) 
@PrepareForTest({JsonAppMessageProcessor.class}) 
public class TestPropStoreAppController { 
    @Test public void testSaveJsonAppTagChangesToPropStore() throws Exception { 
     JsonAppMessageProcessor messageProcessorSpy = spy(new JsonAppMessageProcessor()); 
     when(messageProcessorSpy, "getRootTagMap", any(JsonAppTag.class)).thenReturn(constructReturnValue()); 
     // I tried it this way too... 
     // doReturn(constructReturnValue()).when(messageProcessorSpy, "getRootTagMap", any(JsonAppTag.class)); 
     // the following call calls the real getRootTagMap(JsonAppTag) method instead of returning the stub 
     messageProcessorSpy.saveChanges(constructParameterForChanges()); 
    } 
} 
+0

혹시 문제가 무엇인지 알아 냈습니까? 지금 당장 같은 문제가있어 무엇을해야할지 모르겠다. https://stackoverflow.com/questions/38155092/powermockito-is-calling-the-method-when-i-use-doreturn-when – Ahmad

답변

1

난 당신이 사용하고있는 PowerMockito 버전이 무엇인가 잘 모릅니다 만, 다음과 같은 시나리오가 나를 위해 작동 : 방법의

doReturn(constructReturnValue()).when(messageProcessorSpy).getRootTagMap(any(JsonAppTag.class)); 

호출을 당신은 스파이 개체를 조롱하고 있습니다 when 메서드에 의해 반환 된 인스턴스에서 호출해야하며 그 안에는 일반 mock 개체 좋아하지 않아요.

관련 문제