Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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_Junit_Mocking_Jmockit - Fatal编程技术网

Java 使用JMockit模拟资源加载

Java 使用JMockit模拟资源加载,java,junit,mocking,jmockit,Java,Junit,Mocking,Jmockit,我的应用程序使用一个保存配置设置的单例。代码如下: private PropertiesSingleton() throws Exception { InputStream appstream = this.getClass().getClassLoader() .getResourceAsStream("application.properties"); props = new Properties(); try { props.

我的应用程序使用一个保存配置设置的单例。代码如下:

private PropertiesSingleton() throws Exception {
    InputStream appstream = this.getClass().getClassLoader()
            .getResourceAsStream("application.properties");
    props = new Properties();
    try {
        props.load(appstream);
    } catch (IOException e) {
        logger.log(Log.FATAL,
                "Cannot find application.properties in classpath.", e);
        throw e;
    }
}
我的应用程序通常在容器中运行。对于我的单元测试,我必须使application.properties可供加载。我这样试过:

@Before
public void init() throws Exception {


    final FileInputStream inStream = new FileInputStream("../path/to/config/application.properties");
    new NonStrictExpectations(ClassLoader.class) {
        {
            String.class.getClassLoader().getResourceAsStream("application.properties"); result = inStream;
        }
    };
}
测试不会启动,但会打印此堆栈跟踪:

java.lang.VerifyError
at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
at de.lpm.ejb.archiving.ArchiveHandlerBeanTest$2.<init>(ArchiveHandlerBeanTest.java:50)
at de.lpm.ejb.archiving.ArchiveHandlerBeanTest.init(ArchiveHandlerBeanTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我认为不可能模仿类加载器,因为它是第一个。在类加载器出现之前,您不能做任何事情。所以试着模仿调用者,而不是类加载器。

我知道这很旧,但我想我仍然可以插话。您只需将其提取到您自己的加载程序类中:

InputStream appstream = this.getClass().getClassLoader()
        .getResourceAsStream("application.properties");
与此类似:

public class MyResourceLoader {
    public InputStream getResource(String location){
        this.getClass().getClassLoader().getResourceAsStream();
    }
}
然后,您只需使用新的MyResourceLoader,然后在测试中将其标记为@Mocked。我希望这有帮助