java 最终类中的 Powermock 静态最终方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45034464/
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
Powermock static final method in final class
提问by Mandar Pandit
The Test case I am writing for:
我正在编写的测试用例:
public class AClassUnderTest {
// This test class has a method call
public Long methodUnderTest() {
// Uses the FinalUtilityClass which contains static final method
FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>);
// I want to mock above call so that test case for my "methodUnderTest" passes
}
}
I have one final class.
我有最后一节课。
public final class FinalUtilityClass {
/**
* Method has 3 parameters
*/
public static final MyBean myStaticFinalMethod(<3-parameters-here>) {
}
}
I have already added below code in my test class:
我已经在我的测试类中添加了以下代码:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ FinalUtilityClass.class })
I want to write test case for mocking it.
I want to mock the call of myStaticFinalMethod()
so that I can get the expected MyBean
instatnce which I can use in further code to pass my test case.
我想编写测试用例来模拟它。我想模拟调用,myStaticFinalMethod()
以便我可以获得预期的MyBean
实例,我可以在进一步的代码中使用它来通过我的测试用例。
The <3-parameters-here>
are Calendar, String, String.
的<3-parameters-here>
是日历,字符串,字符串。
I tried doing:
我试着做:
1)
1)
PowerMockito.mock(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
2)
2)
PowerMockito.mockStatic(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
3)
3)
PowerMockito.spy(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
But nothing worked for me. Please suggest what is correct way for mocking static final
method in final
class.
但没有什么对我有用。请建议static final
在final
课堂上模拟方法的正确方法是什么。
回答by GhostCat
The following stepsare required to mock calls to static methods:
模拟对静态方法的调用需要执行以下步骤:
- Use the
@RunWith(PowerMockRunner.class)
annotation at the class-level of the test case. - Use the
@PrepareForTest(ClassThatContainsStaticMethod.class)
annotation at the class-level of the test case - Use
PowerMock.mockStatic(ClassThatContainsStaticMethod.class)
to mock all methods of this class
@RunWith(PowerMockRunner.class)
在测试用例的类级别使用注释。@PrepareForTest(ClassThatContainsStaticMethod.class)
在测试用例的类级别使用注解- 使用
PowerMock.mockStatic(ClassThatContainsStaticMethod.class)
嘲笑这个类的所有方法
When you follow these steps as documented, your tests should work. And as the OP seems to be confused about PowerMock vs. PowerMockito - that is (more or less) the same thing:
当您按照文档中的这些步骤进行操作时,您的测试应该可以正常工作。由于 OP 似乎对 PowerMock 与 PowerMockito 感到困惑 - 这(或多或少)是同一件事:
PowerMock and PowerMockito are based on the sametechnology. They just have different "connectors" to either work with EasyMock or Mockito. So, yes the above example says PowerMock.mockStatic()
- but PowerMockitohas mockStatic()
methods as well. In that sense: the corethings (for example regarding preparationwith annotations) are the same. See herefor example (they are so close that the linked tutorial says "Intro to PowerMock" - although it does introduce PowerMockito.
PowerMock 和 PowerMockito 基于相同的技术。他们只是有不同的“连接器”可以与 EasyMock 或 Mockito 一起使用。所以,是的,上面的例子说PowerMock.mockStatic()
- 但PowerMockito也有mockStatic()
方法。从这个意义上说:核心内容(例如关于使用注释的准备)是相同的。例如,请参见此处(它们非常接近,链接的教程说“PowerMock 简介”-尽管它确实介绍了 PowerMockito。
And as you seem to not believe me - see this example:
正如你似乎不相信我 - 看这个例子:
package ghostcat.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
final class ClassWithStatic {
public final static int ignoreMethodCall(String a, String b, int c) {
System.out.println("SHOULD NOT SHOW UP: " + a);
return c;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithStatic.class)
public class MockStaticTest {
@Test
public void test() {
PowerMockito.mockStatic(ClassWithStatic.class);
PowerMockito.when(ClassWithStatic.ignoreMethodCall("a", "b", 5)).thenReturn(42);
org.junit.Assert.assertEquals(ClassWithStatic.ignoreMethodCall("a", "b", 5), 42);
}
}
This test passes; and doesn't print anything. Therefore the final static method gets mocked.
此测试通过;并且不打印任何内容。因此最终的静态方法被嘲笑。