我有一个方法,将InputStream作为参数.该方法从InputStream中提取Properties并返回"version"属性.此属性包含应用程序的版本.
public String getVersion(InputStream inputStream) throws IOException {
Properties properties = new Properties();
properties.load(inputStream);
String version = properties.getProperty("version");
return version;
}
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我想创建一个Properties对象,设置一些属性,然后将属性加载到InputStream中.最后,InputStream将传递给测试中的方法.
@Test
public void testGetAppVersion() {
InputStream inputStream = null;
Properties prop = new Properties();
prop.setProperty("version", "1.0.0.test");
// Load properties into InputStream
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
您可以使用该store
方法将属性写入流.
这是一个使用字节数组流的示例:
Properties prop = new Properties();
prop.put("version", "1.0.0.test");
ByteArrayOutputStream os = new ByteArrayOutputStream();
props.store(os, "comments");
InputStream s = new ByteArrayInputStream(os.toByteArray());
String version = getVersion(s);
Run Code Online (Sandbox Code Playgroud)
但我相信以下方式更简单(从字符串文件内容输入流):
InputStream is =
new ByteArrayInputStream("version=1.0.0.test\n".getBytes());
Run Code Online (Sandbox Code Playgroud)