Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JMockit模拟初始化期间引发异常的类_Java_Mocking_Jmockit - Fatal编程技术网

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
安全内容