我的应用程序使用一个包含配置设置的单例。这是代码:
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)