PowerMockito间谍运行实际的私有方法

时间:2014-09-30 15:17:42

标签: java junit mocking

@RunWith(PowerMockRunner.class)
@PrepareForTest( ClassWithPrivate.class  )
public class Testy{

   @Test
   public void testSOmething(){
         String response = "something";
        ClassWithPrivate  some = PowerMockito.spy(new ClassWithPrivate("someArg"));
        PowerMockito.doReturn(response).when(some,PowerMockito.method(ClassWithPrivate.class,"privateMethod",String.class)).withArguments(anyString();

    }
}

我对PowerMockito不太熟悉,但这是正常的,当doReturn线运行时,它将实际调用privateMethod。

对我来说,问题是我想模仿privateMethod,因为没有嘲笑它会抛出异常。目前我的测试将在doReturn行之后关闭,因为privateMethod会抛出异常。

1 个答案:

答案 0 :(得分:0)

尝试使用:

PowerMockito.doReturn(response).when(some, "privateMethod", anyString());