相关疑难解决方法(0)

在JUnit @BeforeClass中加载属性文件

我正在尝试在我的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);**
    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

输出:

[junit] Tests run: 0, Failures: 0, Errors: 1, Time elapsed: 0.104 sec
[junit] 
[junit] …
Run Code Online (Sandbox Code Playgroud)

java ant junit

10
推荐指数
1
解决办法
3万
查看次数

标签 统计

ant ×1

java ×1

junit ×1