【问题标题】:Loading Properties File In JUnit @BeforeClass在 JUnit @BeforeClass 中加载属性文件
【发布时间】:2012-04-16 12:38:00
【问题描述】:

我在执行 JUnit 测试期间尝试从我的类路径加载 sample.properties,但它在类路径中找不到该文件。如果我编写一个 Java Main 类,我就可以很好地加载文件。我正在使用下面的 ant 任务来执行我的 JUnit。

public class Testing {
 @BeforeClass
    public static void setUpBeforeClass() throws Exception {   
        Properties props = new Properties();
        InputStream fileIn = props_.getClass().getResourceAsStream("/sample.properties");
        **props.load(fileIn);**
    }

}

JUnit:

<path id="compile.classpath">
        <pathelement location="${build.classes.dir}"/>
    </path>
    <target name="test" depends="compile">
            <junit haltonfailure="true">
                <classpath refid="compile.classpath"/>
                <formatter type="plain" usefile="false"/>
                <test name="${test.suite}"/>
            </junit>
        </target>
        <target name="compile">
            <javac srcdir="${src.dir}" 
                   includeantruntime="false"
                   destdir="${build.classes.dir}" debug="true" debuglevel="lines,vars,source">
                <classpath refid="compile.classpath"/>
            </javac>
            <copy todir="${build.classes.dir}">
                <fileset dir="${src.dir}/resources"
                         includes="**/*.sql,**/*.properties" />
            </copy>
        </target>

输出:

[junit] Tests run: 0, Failures: 0, Errors: 1, Time elapsed: 0.104 sec
[junit] 
[junit] Testcase: com.example.tests.Testing took 0 sec
[junit]     Caused an ERROR
[junit] null
[junit] java.lang.NullPointerException
[junit]     at java.util.Properties$LineReader.readLine(Properties.java:418)
[junit]     at java.util.Properties.load0(Properties.java:337)
[junit]     at java.util.Properties.load(Properties.java:325)
[junit]     at com.example.tests.Testing.setUpBeforeClass(Testing.java:48)
[junit]

【问题讨论】:

    标签: java ant junit


    【解决方案1】:

    这行得通:

    YourTestClass.class.getClassLoader().getResourceAsStream

    不起作用

    YourTestClass.class.getResourceAsStream

    【讨论】:

      【解决方案2】:

      您需要将${build.classes.dir} 添加到compile.classpath

      更新:根据 cmets 中的沟通,原来classpath 不是问题。而是使用了错误的类加载器。

      Class.getResourceAsStream() 根据加载类的类加载器查找资源的路径。事实证明,Properties 类是由与Testing 类不同的类加载器加载的,并且资源路径与该类加载器的classpath 的关系不正确。解决方案是使用Testing.class.getResourceAsStream(...) 而不是Properties.class.getResourceAsStream(...)

      【讨论】:

      • 感谢您的回复。我添加了包含 ${build.classes.dir} 的 compile.classpath,它解析为上面的 build/classes 目录,因为它已经像你建议的那样,所以不幸的是这不是我的问题。
      • 这可能发生的唯一其他时间 (AFAIK) 是当您尝试从由与您自己的类不同的类加载器加载的类中加载资源时。尝试getClass().getReasourceAsStream(...) 而不是prop.getClass().getResourceAsStream(...)。让我知道这是否能解决您的问题,我会更新答案
      • InputStream is = Testing.class.getClassLoader().getResourceAsStream("sample.properties");成功了,谢谢你的建议。
      • 我已经更新了我的答案。请接受它,因为它现在包含您问题的解决方案。
      猜你喜欢
      • 2012-02-19
      • 2018-10-08
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多