java 如何使用 PowerMock 部分模拟公共方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10758166/
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-10-31 02:26:18  来源:igfitidea点击:

how to partial mock public method using PowerMock?

javajunitpowermockpartial-mocks

提问by maverick

Following is my class

以下是我的课

public class SomeClass {
    public ReturnType1 testThisMethod(Type1 param1, Type2 param2) {
        //some code
        helperMethodPublic(param1,param2);
        //more code follows  
    }   

    public ReturnType2 helperMethodPublic(Type1 param1, Type2 param2) {
        //some code            
    }
} 

So in the above class while testing testThisMethod(), I want to partially mock helperMethodPublic().

所以在上面的类中,在测试 testThisMethod() 时,我想部分模拟 helperMethodPublic()。

As of now, I am doing the following:

截至目前,我正在执行以下操作:

SomeClass someClassMock = 
    PowerMock.createPartialMock(SomeClass.class,"helperMethodPublic");
PowerMock.expectPrivate(someClassMock, "helperMethodPublic, param1, param2).
    andReturn(returnObject);

The compiler doesn't complain. So I try to run my test and when the code hits the helperMethodPublic() method, the control goes into the method and starts to execute each line of code in there. How do I prevent this from happening?

编译器不会抱怨。所以我尝试运行我的测试,当代码命中 helperMethodPublic() 方法时,控件进入该方法并开始执行其中的每一行代码。我如何防止这种情况发生?

回答by pedorro

Another solution that doesn't rely on a mock framework would be to override 'helperMethodPublic' in an anonymous subclass defined within your test:

另一个不依赖于模拟框架的解决方案是在您的测试中定义的匿名子类中覆盖“helperMethodPublic”:

SomeClass sc = new SomeClass() {
   @Override
   public ReturnType2 helperMethodPublic(Type1 p1, Type2 p2) {
      return returnObject;
   }
};

Then when you use this instance in your test it will run the original version of 'testThisMethod' and the overridden version of 'helperMethodPublic'

然后,当您在测试中使用此实例时,它将运行“testThisMethod”的原始版本和“helperMethodPublic”的覆盖版本

回答by psb

I think it is because of what Jeff said.

我认为这是因为杰夫所说的。

Try this - setting up an expectation just as any other mocked method:

试试这个 - 就像任何其他模拟方法一样设置期望:

SomeClass someClassMock = PowerMock.createPartialMock(SomeClass.class,
                                                      "helperMethodPublic");

EasyMock.expect(someClassMock.helperMethodPublic(param1, param2)).
    andReturn(returnObject);

PowerMock.replayAll();

回答by jeff

I would guess this is because your "helperMethodPublic" is not a private method (as in PowerMock.expectPrivate). PowerMock is a framework that extends other mocking frameworks to add things such as mocking private and static methods (which JMock, Mockito, etc don't handle). Doing a partial mock of public methods should be something your underlying mock framework handles.

我猜这是因为您的“helperMethodPublic”不是私有方法(如在 PowerMock.expectPrivate 中)。PowerMock 是一个框架,它扩展了其他模拟框架以添加诸如模拟私有和静态方法(JMock、Mockito 等不处理)之类的内容。对公共方法进行部分模拟应该是您的底层模拟框架处理的事情。