Java JMockit模拟初始化期间引发异常的类
例如,我一直在尝试创建一个模拟的Java JMockit模拟初始化期间引发异常的类,java,mocking,jmockit,Java,Mocking,Jmockit,例如,我一直在尝试创建一个模拟的oracle.adf.share.security.identitymanagement.UserProfile @Test public void testMyTest(@Mocked final UserProfile userProfile) { new Expectations() { { userProfile.getBusinessEmail(); result = "me@me.co
oracle.adf.share.security.identitymanagement.UserProfile
@Test
public void testMyTest(@Mocked final UserProfile userProfile) {
new Expectations() {
{
userProfile.getBusinessEmail();
result = "me@me.com";
}
};
unitUnderTest.methodUnderTest();
}
然而,我发现当JMockit试图构造模拟时会抛出一个异常
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at oracle.jdevimpl.junit.runner.junit4.JUnit4Testable.run(JUnit4Testable.java:24)
at oracle.jdevimpl.junit.runner.TestExecution.run(TestExecution.java:27)
at oracle.jdevimpl.junit.runner.JUnitTestRunner.main(JUnitTestRunner.java:88)
Caused by: oracle.adf.share.security.ADFSecurityRuntimeException: EXC_FAILED_ID_STORE
at oracle.adf.share.security.identitymanagement.UserManager.<init>(UserManager.java:111)
at oracle.adf.share.security.identitymanagement.UserManager.<init>(UserManager.java:83)
at oracle.adf.share.security.identitymanagement.UserProfile.<clinit>(UserProfile.java:62)
UserManager
构造函数如下所示
public class UserProfile implements Serializable {
...
private static UserManager _usrMgr = new UserManager();
...
}
public UserManager() {
this((String) null);
}
public UserManager(String providerClassName) {
if (providerClassName != null) {
...
} else {
String clzName = ADFSecurityUtil.getIdentityManagementProviderClassName();
if (clzName != null) {
IdentityManagement provider = (IdentityManagement) createObject(clzName);
setIdentityManagementProvider(provider);
} else {
throw new ADFSecurityRuntimeException("EXC_FAILED_ID_STORE");
}
}
}
现在,我可以在ADFSecurityUtil.getIdentityManagementProviderClassName()时设置一个返回内容的期望
被调用,但是当它试图加载这个类时,我预见到了进一步的问题
解决此问题的最佳方法是什么?好的,所以我设法找到了解决方案!这就是我所做的
@Test
public void testMyTest() {
new Mockup<UserProfile>() {
@Mock
public void $cinit() {
// do nothing
}
@Mock
public String getBusinessEmail() {
return "me@me.com";
}
};
unitUnderTest.methodUnderTest();
}
@测试
公共void testMyTest(){
新模型(){
@嘲弄
公开作废$cinit(){
//无所事事
}
@嘲弄
公共字符串getBusinessEmail(){
返回“me@me.com";
}
};
unitUnderTest.methodUnderTest();
}
当然,最好的解决方案是不要模仿UserProfile
类。有什么原因不能实例化它并用所需的“businessEmail”值填充它吗?因为我会遇到与UserManager
static相同的问题,它需要一个运行的容器,其中设置了Oracle ADF
安全内容