最终类中的Powermock静态最终方法 [英] Powermock static final method in final class
问题描述
我写的测试案例:
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
}
}
我有一个最后一堂课。
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 })
我想写一个测试用例来嘲笑它。
我想模拟 myStaticFinalMethod()
的调用,这样我就可以获得预期的 MyBean
instatnce我可以在进一步的代码中使用我的测试用例。
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.
< 3-parameters-here>
是日历,字符串,字符串。
The <3-parameters-here>
are Calendar, String, String.
我尝试过 :
I tried doing:
1)
PowerMockito.mock(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
2)
PowerMockito.mockStatic(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
3)
PowerMockito.spy(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
但对我来说没什么用。请建议在 final
类中模拟静态最终
方法的正确方法。
But nothing worked for me. Please suggest what is correct way for mocking static final
method in final
class.
推荐答案
以下步骤模拟对静态方法的调用是必需的:
The following steps are required to mock calls to static methods:
- 使用
@RunWith(PowerMockRunner.class)$ c测试用例的类级别的$ c>注释。
- 使用
@PrepareForTest(ClassThatContainsStaticMethod.class)
注释测试用例的类级别 - 使用
PowerMock.mockStatic(ClassThatContainsStaticMethod.class)
来模拟此类的所有方法
- 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
当您按照记录的步骤执行这些步骤时,您的测试应该有效。由于OP似乎对PowerMock和PowerMockito感到困惑 - 这或多或少是相同的事情:
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:
PowerMock和PowerMockito基于相同的技术。他们只是使用不同的连接器来使用EasyMock或Mockito。所以,是的,上面的例子说 PowerMock.mockStatic()
- 但 PowerMockito mockStatic()
方法。从这个意义上说:核心事物(例如关于带注释的准备)是相同的。例如,请参见此处(它们非常接近,以至于链接的教程说PowerMock简介 - 虽然它确实引入了PowerMockito。
PowerMock and PowerMockito are based on the same technology. They just have different "connectors" to either work with EasyMock or Mockito. So, yes the above example says PowerMock.mockStatic()
- but PowerMockito has mockStatic()
methods as well. In that sense: the core things (for example regarding preparation with annotations) are the same. See here for example (they are so close that the linked tutorial says "Intro to PowerMock" - although it does introduce 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.
这篇关于最终类中的Powermock静态最终方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!