Java 使用PowerMock Mockito的部分Mock私有方法

Java 使用PowerMock Mockito的部分Mock私有方法,java,junit,mockito,powermock,private-methods,Java,Junit,Mockito,Powermock,Private Methods,我不熟悉Mockito和PowerMock。我需要测试一些遗留代码,它有一个我必须模拟的私有方法。我正在考虑使用PowerMock中的私有部分模拟功能,我试图模拟来自的示例,但失败了。我不知道它怎么了。你能帮我查一下吗?谢谢 下面是要测试的类: package test; public class ClassWithPrivate { private String getPrivateString() { return "PrivateString"; }

我不熟悉Mockito和PowerMock。我需要测试一些遗留代码,它有一个我必须模拟的私有方法。我正在考虑使用PowerMock中的私有部分模拟功能,我试图模拟来自的示例,但失败了。我不知道它怎么了。你能帮我查一下吗?谢谢

下面是要测试的类:

package test;

public class ClassWithPrivate
{

     private String getPrivateString() {
       return "PrivateString";
     }

     private String getPrivateStringWithArg(String s) {
       return "PrivateStringWithArg";
     }

 }
这是测试代码:

package test;

import static org.mockito.Mockito.*;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.api.support.membermodification.MemberMatcher;
import org.powermock.modules.junit4.PowerMockRunner;

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

    @Test
    public void testGetPrivateString() {

         ClassWithPrivate spy = PowerMockito.spy(new ClassWithPrivate());

         PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());

    }

}
编辑 当我试图编译代码时,它失败了,出现以下错误:

ClassWithPrivateTest.java:26: unreported exception java.lang.Exception; must be caught or declared to be thrown
     PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());
                                     ^
ClassWithPrivateTest.java:26: unreported exception java.lang.Exception; must be caught or declared to be thrown
     PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());

我发现了问题,测试方法期望出现异常。在我修改它如下,它是工作良好

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

   @Test
   public void testGetPrivateString() throws Exception {

     ClassWithPrivate spy = PowerMockito.spy(new ClassWithPrivate());

     PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());

   }

}

我发现了问题,测试方法期望出现异常。在我修改它如下,它是工作良好

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

   @Test
   public void testGetPrivateString() throws Exception {

     ClassWithPrivate spy = PowerMockito.spy(new ClassWithPrivate());

     PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());

   }

}

对我来说,测试工作如预期。你说“它失败了”是什么意思。运行测试时是否存在异常?然后把它贴在这里。@ChristopherRoscoe:嗨,当我编译它时,它给了我如上所述的错误。你编译成功了吗?谢谢我,测试按预期进行。你说“它失败了”是什么意思。运行测试时是否存在异常?然后把它贴在这里。@ChristopherRoscoe:嗨,当我编译它时,它给了我如上所述的错误。你编译成功了吗?谢谢