接口测试中用ant运行build.xml时,提示找不到*.properties文件,而在eclipse中运行则是没有问题的。
详情:读取*.config的相应代码如下:
private static URL filePath=Thread.currentThread().getContextClassLoader().getResource("");
private static String fileName = "*.properties";
public ConfigUtil(){}
private static Properties props = new Properties();
static{
try {
props.load(new FileInputStream(filePath.getPath()+File.separator+fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
相应的build.xml代码如下:
<!-- property 定义全局变量-->
<property name="testng.file" value="testng.xml"/>
<property name="src.dir" value="src"/>
<property name="class.dir" value="build/class"/>
<property name="lib.dir" value="build/lib"/>
<property name="report.dir" value="test-output"/>
<property name="report_rar.dir" value="report_rar"/>
<property name="testngoutput.dir" value="test-output"/>
<taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.5.1.jar"/>
<!-- 编译 -->
<target name="compiles" depends="init" description="compile the source files">
<javac srcdir="${src.dir}" destdir="${class.dir}" classpathref="classes" includeantruntime="on">
<compilerarg line="-encoding UTF-8 "/>
<classpath refid="Test-classpath"/>
</javac>
</target>
<!-- 运行测试 -->
<target name="regression" depends="compiles">
<testng outputdir="${testngoutput.dir}" classpathref="classes" delegateCommandSystemProperties="true">
<classpath refid="Test-classpath"/>
<xmlfileset dir="${basedir}" includes="${testng.file}"/>
</testng>
</target>
ant运行时的class路径是:工程路径/build/calss,所以提示:工程路径/build/calss/*.properties文件找不到。
暂时解决方法:在build.xml文件的初始化中添加语句:<copy file=" " todir=" "/>
应该是在eclipse中和用ant运行的classpath不同,导致到不到配置文件,有时间再详细了解一下区别,此文件先暂记于此。