java 使用 PowerMock-Mockito 部分模拟私有方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17733254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:48:05  来源:igfitidea点击:

Partial Mock Private Method with PowerMock-Mockito

javajunitmockitopowermockprivate-methods

提问by wlhee

I am new to Mockito and PowerMock. I need to test some legacy code which has a private method I have to mock. I am considering using the private partial mocking feature from PowerMock, I tried to mimic the example from the link, but it failed. I have no idea what's wrong with it. Could you help to check it? Thanks

我是 Mockito 和 PowerMock 的新手。我需要测试一些遗留代码,这些代码有一个我必须模拟的私有方法。我正在考虑使用 PowerMock 的私有部分模拟功能,我试图模仿链接中的示例,但它失败了。我不知道有什么问题。你能帮忙检查一下吗?谢谢

Here's the class to-be-tested:

这是要测试的类:

package test;

public class ClassWithPrivate
{

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

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

 }

And This is the Test Code:

这是测试代码:

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());

    }

}

EDITWhen I tried to compile the code, it failed with the following errors:

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

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());

回答by wlhee

I found out the problem, the test methods expects a exception. After I modified it as follows, it is working fine.

我发现了问题,测试方法需要一个异常。经过我如下修改后,它工作正常。

@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());

   }

}