简体   繁体   English

如何使用Moxie模拟静态方法

[英]How to mock static methods using Moxie

How can we mock static methods with Moxie framework (I should use only Moxie, because my organization allows only moxie in their environment.) 我们如何使用Moxie框架模拟静态方法(我应该仅使用Moxie,因为我的组织在其环境中仅允许使用moxie。)

Suppose I have a class 假设我有一堂课

public class Test{
  public int x;
  public int y;
  public static int add(){
    return x + y;
  }
}

I have to mock add class of Test using Moxie framework. 我必须使用Moxie框架模拟添加Test类。

If I use like this 如果我这样使用

Test t = Moxie.mock(Test.class);
Moxie.stub(t).willReturn(20).when(t.add());

I get a warning saying that static method add should be accessed from type Test in a static way and also mocking does not happen when I run the test. 我收到一条警告,指出应该以静态方式从Test类型访问静态方法add,并且在运行测试时也不会发生模拟。

Please help me to resolve this issue. 请帮助我解决此问题。

You can use PowerMock along with moxie to mock static classes. 您可以将PowerMock与moxie一起使用以模拟静态类。

See http://moxiemocks.org/mvn/apidocs/index.html?moxie/MoxieRunner.html . 请参阅http://moxiemocks.org/mvn/apidocs/index.html?moxie/MoxieRunner.html

HTH, Keshava. HTH,喀沙瓦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM