I have a method that takes an InputStream as a parameter. The method extracts Properties from the InputStream and returns the "version" property. This property holds the version of the application.
public String getVersion(InputStream inputStream) throws IOException {
Properties properties = new Properties();
properties.load(inputStream);
String version = properties.getProperty("version");
return version;
}
For testing purposes, I'd like to create a Properties object, set some properties, and then load the properties into an InputStream. Finally, the InputStream would be passed to the method under test.
@Test
public void testGetAppVersion() {
InputStream inputStream = null;
Properties prop = new Properties();
prop.setProperty("version", "1.0.0.test");
// Load properties into InputStream
}
How would I go about this?
You can use the store
method to write the properties to a stream.
Here's an example that uses byte array streams:
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);
But I believe the following way is simpler (input stream from string file content):
InputStream is =
new ByteArrayInputStream("version=1.0.0.test\n".getBytes());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With