Mockito mock of SecurityManager throwing an exception(SecurityManager 的 Mockito 模拟抛出异常)
问题描述
我正在尝试模拟 SecurityManager
类.当我运行以下代码时,Mockito 会抛出异常:
I'm trying to mock the SecurityManager
class. When I run the following code, Mockito throws an exception:
@After
public void tearDown()
{
SecurityManager securityManagerMock = mock(SecurityManager.class);
System.setSecurityManager(securityManagerMock);
}
堆栈跟踪是以下几行无限重复:
The stack trace is the following lines repeated indefinitely:
at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:42)
at $java.lang.SecurityManager$$EnhancerByMockitoWithCGLIB$$3ceafc0f.checkMemberAccess(<generated>)
at java.lang.Class.checkMemberAccess(Class.java:2157)
at java.lang.Class.getDeclaredField(Class.java:1879)
at org.mockito.internal.creation.cglib.CGLIBHacker.reflectOnCreateInfo(CGLIBHacker.java:44)
at org.mockito.internal.creation.cglib.CGLIBHacker.setMockitoNamingPolicy(CGLIBHacker.java:20)
at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:42)
at $java.lang.SecurityManager$$EnhancerByMockitoWithCGLIB$$3ceafc0f.checkMemberAccess(<generated>)
at java.lang.Class.checkMemberAccess(Class.java:2157)
at java.lang.Class.getDeclaredField(Class.java:1879)
at org.mockito.internal.creation.cglib.CGLIBHacker.reflectOnCreateInfo(CGLIBHacker.java:44)
at org.mockito.internal.creation.cglib.CGLIBHacker.setMockitoNamingPolicy(CGLIBHacker.java:20)
at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:42)
at $java.lang.SecurityManager$$EnhancerByMockitoWithCGLIB$$3ceafc0f.checkMemberAccess(<generated>)
at java.lang.Class.checkMemberAccess(Class.java:2157)
at java.lang.Class.getDeclaredField(Class.java:1879)
at org.mockito.internal.creation.cglib.CGLIBHacker.reflectOnCreateInfo(CGLIBHacker.java:44)
at org.mockito.internal.creation.cglib.CGLIBHacker.setMockitoNamingPolicy(CGLIBHacker.java:20)
我在这里做错了什么?
推荐答案
PS!您还可以模拟对 getSecurityManager() 方法的静态方法调用.
PS! You could also mock static method call to getSecurityManager() method.
模拟静态方法请参阅 http://code.google.com/p/powermock/wiki/MockitoUsage
在类级别添加@PrepareForTest.
Add @PrepareForTest at class level.
@PrepareForTest(System.class); // System.class contains static methods
调用 PowerMockito.mockStatic() 模拟静态类(使用 PowerMockito.mockStaticPartial(class, method) 模拟特定方法):
Call PowerMockito.mockStatic() to mock a static class (use PowerMockito.mockStaticPartial(class, method) to mock a specific method):
PowerMockito.mockStatic(System.class);
只需使用 Mockito.when() 来设置您的期望:
Just use Mockito.when() to setup your expectation:
Mockito.when(System.getSecurityManager()).thenReturn(securityManagerMock);
这篇关于SecurityManager 的 Mockito 模拟抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SecurityManager 的 Mockito 模拟抛出异常
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01