mardi 26 avril 2016

How to test IOException and IllegalArgument Exception for Properties.load and getResourceasStream

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.
 * 
 * @param propertiesPath
 * @throws IOException
 */

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

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

}

/**
 * Gets the value for foo from the properties file based on the
 * foo  as parameter.
 * 
 * @param -
 *            foo 
 * @return - foo value of foo in the properties file.
 * @throws IllegalArgumentException
 */
public String getfoo(String foo) {
Preconditions.checkArgument(StringUtils.isNotBlank(foo),
    ResourceAssistant.getString(PropertiesRetriever.class, "fooNullMessage"));
this.foo = properties.getProperty(foo);
return foo;
}

/**
 * Gets the value for foo1 from the properties file based on the
 * foo1 for repository as parameter.
 * 
 * @param -
 *            foo1
 * @return - foo1 value of foo1 in the properties file.
 * @throws IllegalArgumentException
 */
public String getfoo1(String foo1) {
Preconditions.checkArgument(StringUtils.isNotBlank(foo1),
    ResourceAssistant.getString(PropertiesRetriever.class, "foo1NullMessage"));
this.foo1 = properties.getProperty(foo1);
return foo1;
}

}

Here, two methods:

 properties.load(in)

and

this.getClass().getReqourceAsStream

throws IOException and IllegalArgumentException. I wanted to test these methods in JUnit testing. Is there anyway I can call these methods as this.class() returns the current class and when I tried to use doThrow in properties.load I got error of Overloading by mockito.

Aucun commentaire:

Enregistrer un commentaire