我想測試IOException
和IllegalArgumentException
方法引發的properties.load(in)
。根據這裏的文檔OracleDoc它說加載方法拋出IOException
- 如果從輸入流中讀取時發生錯誤。 IllegalArgumentException
- 如果輸入流包含格式錯誤的Unicode轉義序列。如何模擬Inputstream來加載Java中的屬性
這裏是我的代碼:
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);
}
這裏,方法:
properties.load(in)
拋出IOException
和IllegalArgumentException
。我想在JUnit測試中測試這個方法。無論如何,我可以稱之爲這些方法。
[嘲諷的Java的InputStream(http://stackoverflow.com/questions/6371379/mocking-java-inputstream) –