How to test the below java catch block where in catch() doesnt throw exception back.
class ErrorTransImpl {
private static final Logger logger = Logger.getLogger(ErrorTransImpl.class);
public int errorCatcher(ErrorTrans transError){
int ct = 0;
if (transError != null){
String query = "INSERT INTO tab_1 (rw1,rw2,rw3,rw4,rw5) VALUES (?,?,?,?,?)";
try {
ct = jdbcTemplate.update(query, new Object[] {transError.col1(),transError.col2(), transError.col3(),transError.col4(),transError.col5()});
}catch (DataAccessException ex) {
logger.error(ex);
}
}
return ct;
}
}
I tried testing as below, but: 1>Unable to get into catch block. 2> Unable to test catch() even if inside as it doesnt throw exception back.
def 'throw DataAccess Exception upon incorrect update'() {
given:
def log = Mock(Logger)
def originalLogger = ErrorTransImpl.logger
ErrorTransImpl.logger = log
ErrorTransImpl errTransImpl = Spy() {
jdbcTemplate >> {
throw new DataAccessException() {
@Override
String getMessage() {
return super.getMessage()
}
}
}
}
when:
errTransImpl.errorCatcher(new ErrorTrans())
then:
// thrown DataAccessException
//Not sure what to assert or test here ??
}
Can anyone help on how i test this?