Reputation: 130
I want to perform unit test to check file loading in java. I saw some posts on Mockitos doThrow but don't get the implementation of it exactly.
My method looks something like this.
public void loadPropertiesFile(String filepath){
logger.info("Loading properties file");
try{
prop.load(new FileInputStream(filepath));
logger.info("Properties file read");
}catch(IOException e){
e.printStackTrace();
logger.info("Properties file read error");
}
}
I was trying to test it like this but getting error on improper usage of doThrow:
@Test
public void loadPropertiesFileTestTrue(){
Utility util=new Utility();
doThrow(FileNotFoundException.class)
.when(util)
.loadPropertiesFile(null);
}
Upvotes: 0
Views: 217
Reputation: 4799
You can use doThrow()
method on mocked objects only.
You should change your code like this:
@Test
public void loadPropertiesFileTestTrue(){
Utility util=Mockito.mock(Utility.class);
doThrow(FileNotFoundException.class)
.when(util)
.loadPropertiesFile(null);
}
Upvotes: 1