在 JUnit @BeforeClass 中加载属性文件 [英] Loading Properties File In JUnit @BeforeClass
问题描述
我正在尝试在 JUnit 测试执行期间从我的类路径加载 sample.properties,但在类路径中找不到该文件.如果我编写了一个 Java Main 类,我就可以很好地加载文件.我正在使用下面的 ant 任务来执行我的 JUnit.
公共类测试{@课前public static void setUpBeforeClass() 抛出异常 {Properties props = new Properties();InputStream fileIn = props_.getClass().getResourceAsStream("/sample.properties");**props.load(fileIn);**}}
JUnit:
<pathelement location="${build.classes.dir}"/></路径><目标名称=测试"依赖=编译"><junit haltonfailure="true"><classpath refid="compile.classpath"/><formatter type="plain" usefile="false"/><test name="${test.suite}"/></junit></目标><目标名称=编译"><javac srcdir="${src.dir}"includeantruntime="假"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"include="**/*.sql,**/*.properties"/></复制></目标>
输出:
[junit] 测试运行:0,失败:0,错误:1,经过的时间:0.104 秒[junit][junit] 测试用例:com.example.tests.Testing 耗时 0 秒[junit] 导致错误[junit] 空[junit] java.lang.NullPointerException[junit] 在 java.util.Properties$LineReader.readLine(Properties.java:418)[junit] 在 java.util.Properties.load0(Properties.java:337)[junit] 在 java.util.Properties.load(Properties.java:325)[junit] 在 com.example.tests.Testing.setUpBeforeClass(Testing.java:48)[junit]
需要在compile.classpath
中添加${build.classes.dir}
.>
更新:根据评论中的交流,结果证明 classpath
不是问题.相反,使用了错误的类加载器.
Class.getResourceAsStream()
根据加载类的类加载器查找资源的路径.事实证明,Properties
类是由与 Testing
类不同的类加载器加载的,并且与该类加载器的 classpath<相关的资源路径不正确/代码>.解决方案是使用
Testing.class.getResourceAsStream(...)
而不是 Properties.class.getResourceAsStream(...)
.
I'm trying to load sample.properties from my classpath during my JUnit test execution and it can't find the file in the class path. If I write a Java Main class I'm able to load the file just fine. I'm using the below ant task to execute my 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>
Output:
[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]
You need to add ${build.classes.dir}
to compile.classpath
.
Update: Based on communication in the comments, it turned out the classpath
was not the problem. Instead the wrong class loader was used.
Class.getResourceAsStream()
looks up the path of the resource based on the class loader the class was loaded by. As it turns out the Properties
class was loaded by a different class loader than the Testing
class, and the resource path was incorrect with relation to that class loader's classpath
. The solution was to use Testing.class.getResourceAsStream(...)
instead of Properties.class.getResourceAsStream(...)
.
这篇关于在 JUnit @BeforeClass 中加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!