I have a function/method which throws some exception when database is being called. I was writing Junit tests and I was told to have good coverage. So, should I write Junit test which catches exception. I think it don't make any sense, since I have to mock the database to do so.
So whole scenario will be like this:
public <returntype> method(<arguments>) {
//statments
try {
// database call
database.insert(<Argument>);
}
catch {
logger.error();
return error;
}
}
It makes sense only when there is something good in catch statment. But if its only logging, then there is no point having a Junit test for this.
So should, I write these test?
public void test_catch_Exception() {
//setup
database = ..//
when(database)
.calls(insert)
.return(Exception);
try{
//some statements for execution of the method
}
catch(Exception e) {
}
Assert.fail();
}
since I have to mock the database to do so
this is what unit tests are all about. Isolating components under test from certain dependencies. For instance, DB.