加载属性文件在JUnit @BeforeClass [英] Loading Properties File In JUnit @BeforeClass
问题描述
我想我的JUnit测试执行过程中加载从我的类路径sample.properties,它无法找到在类路径中的文件。如果我写一个Java类的主,我能加载文件就好了。我使用下面的蚂蚁任务来执行我的JUnit。
公共类的测试{
@BeforeClass
公共静态无效setUpBeforeClass()抛出异常{
属性道具=新特性();
InputStream的FILEIN = props_.getClass()的getResourceAsStream(/ sample.properties)。
** props.load(FILEIN)**
}}
JUnit的:
<路径ID =compile.classpath>
< pathelement位置=$ {}的build.classes.dir/>
< /路径>
<目标名称=测试依赖=编译>
< JUnit的haltonfailure =真正的>
<类路径REFID =compile.classpath/>
<格式化类型=普通usefile =FALSE/>
<试验NAME =$ {} test.suite/>
< / JUnit的>
< /目标与GT;
<目标名称=编译>
< javac的SRCDIR =$ {}对于src.dir
includeantruntime =假
DESTDIR =$ {}的build.classes.dir调试=真DEBUGLEVEL =行,增值经销商,源>
<类路径REFID =compile.classpath/>
< / javac的>
<副本todir =$ {}的build.classes.dir>
<文件集DIR =$ {}对于src.dir /资源
包括=** / * SQL,** / *属性。/>
< /复制>
< /目标与GT;
输出:
[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的]
您需要添加 $ {}的build.classes.dir
到编译的.classpath
。
更新:基于在评论沟通,原来的的classpath
不是问题。相反,使用了错误的类加载器。
Class.getReasourceAsStream()
查找基于类被加载的类加载器资源的路径。因为它原来的属性
类是由不同的类加载器比测试
类加载,资源路径是不正确的与就该类加载器的类路径中。该解决方案是使用 Testing.class.getReasourceAsStream(...)
而不是 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 classloader was used.
Class.getReasourceAsStream()
looks up the path of the resource based on the classloader the class was loaded by. As it turns out the Properties
class was loaded by a different classloader than the Testing
class, and the resource path was incorrect with relation to that classloader's classpath. The solution was to use Testing.class.getReasourceAsStream(...)
instead of Properties.class.getResourceAsStream(...)
.
这篇关于加载属性文件在JUnit @BeforeClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!