1.首先在测试类中获取context:
public class XXXTest extends AndroidTestCase {
private Properties properties;
private Config config;
private String KEY;
private String SECRET;
@Override
public void setUp() throws Exception{
config = new Config();
Context context = super.getContext();//获取context
properties = config.getFromAssets(context);//获取properties的实例
KEY = properties.getProperty("KEY");//获取.properties文件中的key
}
}
2.通过context读取asserts下面的.properties文件(注意配置文件需要放在asserts目录下或res的raw目录下)获取properties实例:
public class Config {
public Properties getFromAssets(Context context) {
Properties prop = new Properties();
try {
InputStream in = context.getAssets().open("prop.properties");
prop.load(in);
} catch (Exception e) {
e.printStackTrace();
}
return prop;
}
}