我是第一次写TestNG测试用例。我做了一些基础知识。现在我必须为一些复杂的业务逻辑编写测试用例。因此要求是,我与数据库建立连接并获取一些数据,之后我将该数据写入CSV文件。文件名来自属性文件,我们在服务器启动时加载并存储在Map中。因此,写Constants.getProperty("KEY");
会返回文件名。如何在服务器关闭时执行相同的代码。有没有什么办法可以在TestNG中初始化这个Constant的地图,应用程序可以获得相同的引用。例如,
public String getAndWriteData(){
Connection con = OracleUtil.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(sqlQuery);
rs = preparedStatement.executeQuery();
String directory = Constants.getProperty("DIRECTORY_NAME");
String fileName = Constants.getProperty("FILE_NAME");
/*
Constants is a class which has a Map. When tomcat server gets start,
it loads all the property into Map. So when tomcat is running the
Constants is availabe. But when I execute TestCases at the time of
building war same property is not available as tomcat is not started.
*/
File targetFile = new File(directory + File.separator + fileName);
FileWriter fileWriter = new FileWriter(targetFile);
writer = new BufferedWriter(fileWriter);
while (rs.next()) {
//Writing data on CSV
}
}