java – 最终类中的Powermock静态final方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 最终类中的Powermock静态final方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2830字,纯文字阅读大概需要5分钟。
内容图文
![java – 最终类中的Powermock静态final方法](/upload/InfoBanner/zyjiaocheng/806/756984a49731436ca176432028dfedad.jpg)
我正在写的测试案例:
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>) {
}
}
我已经在我的测试类中添加了以下代码:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ FinalUtilityClass.class })
我想写一个测试用例来嘲笑它.
我想模拟myStaticFinalMethod()的调用,以便我可以获得预期的MyBean instatnce,我可以在进一步的代码中使用它来传递我的测试用例.
< 3-parameters-here>是Calendar,String,String.
我试过做:
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());
但没有什么对我有用.请建议在最终类中模拟静态最终方法的正确方法.
解决方法:
模拟对静态方法的调用需要以下steps:
>在测试用例的类级别使用@RunWith(PowerMockRunner.class)注释.
>在测试用例的类级别使用@PrepareForTest(ClassThatContainsStaticMethod.class)注释
>使用PowerMock.mockStatic(ClassThatContainsStaticMethod.class)来模拟此类的所有方法
当您按照文档记录这些步骤时,您的测试应该可行.因为OP似乎对PowerMock和PowerMockito感到困惑 – 这或多或少都是同样的事情:
PowerMock和PowerMockito基于相同的技术.他们只是使用不同的“连接器”来使用EasyMock或Mockito.所以,是的,上面的例子说PowerMock.mockStatic() – 但PowerMockito也有mockStatic()方法.从这个意义上讲:核心事物(例如关于注释的准备)是相同的.例如,参见here(它们非常接近,链接的教程说“PowerMock简介” – 尽管它确实引入了PowerMockito.
而你似乎不相信我 – 看到这个例子:
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);
}
}
该测试通过;并且不打印任何东西.因此最终的静态方法被嘲笑.
内容总结
以上是互联网集市为您收集整理的java – 最终类中的Powermock静态final方法全部内容,希望文章能够帮你解决java – 最终类中的Powermock静态final方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。