mercredi 27 avril 2016

How to Unit Test abstract Inputstream class

I wanted to test the IOException and IllegalArgumentException thrown by properties.load(in) method. As per the documentation here OracleDoc it says the load method throws IOException - if an error occurred when reading from the input stream. IllegalArgumentException - if the input stream contains a malformed Unicode escape sequence.

Here is my code:

public class PropertiesRetriever {

private String foo;
private String foo1;
private Properties properties;

/**
 * Injects the properties file Path in the {GuiceModule}
 * Calls {@link PropertiesRetriever#loadPropertiesPath(String) to load the
 * properties file.
 */

@Inject
public PropertiesRetriever(@Named("propertiesPath") String propertiesPath,         Properties properties)
throws IOException {
this.properties = properties;
loadPropertiesPath(propertiesPath);
}

/**
 * Loads the properties file as inputstream.
 * 
 */
public void loadPropertiesPath(String path) throws IOException {
InputStream in = this.getClass().getResourceAsStream(path);
properties.load(in);

}
Here, a method:

 properties.load(in)

throws IOException and IllegalArgumentException. I wanted to test this methods in JUnit testing. Is there anyway I can call these methods.

I have tried to use ArgumentCaptor to Capture InputStream but I don't know how to verify the captured method:

/**
 * Test to check the IOException while loading the inputstream
 */
@Test(expected = IOException.class)
public void test_PropertiesLoadIOException() throws IOException {
ArgumentCaptor<InputStream> reqCaptor =     ArgumentCaptor.forClass(InputStream.class);
doThrow(IOException.class).when(mockProperties).load(reqCaptor.capture());
propertiesRetreiver.loadPropertiesPath(filePath);
}

Is this a correct way to do this?

Aucun commentaire:

Enregistrer un commentaire