Refactoring test exception handling