嗨,我有一个类ReadProperty
,它有一个ReadPropertyFile
返回类型的方法,Myclass
它从属性文件中读取参数值并返回Myclass
对象。我需要帮助来测试ReadPropertyFile
方法JUnit
,如果可能的话,使用模拟文件和模拟对象。
这是我的代码。
import java.io.FileInputStream;
import java.util.Properties;
public class ReadProperty {
public Myclass ReadPropertyFile(String fileName) {
Myclass myclass = null;
String testparam = null;
FileInputStream fis = null;
Properties prop = new Properties();
try {
fis = new FileInputStream(fileName);
try {
prop.load(fis);
System.out.println("Load Property file : Success !");
} catch (Exception ex) {
System.out.println("Load Property file : Exception : " + ex.toString());
}
/*
* loading the properties
*/
try {
testparam = prop.getProperty("testparam");
System.out.println("testparam Type : " + testparam);
} catch (Exception ex) {
System.out.println("testparam Type : " + ex.toString());
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Property file read fail : " + ex.toString());
System.exit(1);
}
Myclass = new Myclass(testparam);
return Myclass;
} }