Improve exception handling in unit tests