Fix missing exception import and refactor tests