1

我在 jUnit 测试中从 xml 加载属性时遇到问题(相同的代码在没有 jUnit 的情况下运行良好):

public class InternetMapTest {

private Properties properties;

@Before
public void readProperties() {

try {
ClassLoader loader = ClassLoader.getSystemClassLoader();
InputStream stream = loader.getResourceAsStream("analyse.properties.xml");
properties.loadFromXML(stream);

...

如果我运行测试,我会得到

java.lang.NullPointerException
    at xxx.yyy.zzz.analyse.tests.InternetMapTest.readProperties(InternetMapTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...
    ...
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

不幸的是,我无法使用此堆栈跟踪来识别问题。首先,我以为找不到该文件(它位于/src/test/resources)。但是 System.out.println(loader.getResource("analysis.properties.xml")); 返回正确的文件路径。

有人可以帮忙吗?

我在 Ubuntu 12.04 amd64 下使用 STS 2.9.1 和 Maven(插件版本 2.3.1,我认为)和 jUnit 4.10。

预先感谢您,亲切的问候。

4

1 回答 1

1

变量“属性”可能为空,您必须在调用加载函数之前创建它。

于 2012-07-07T09:34:31.287 回答