怎么利用 JUnit 来测试一个方法的异常?
How do you use JUnit to test that a method throws an exception?
题目类型: 技术面试题
这是一道技术面试题,常见于澳洲IT公司面试中。
难度: medium
分类: System Design
标签: JUnit, exceptions, assertThrows, @Test expected
参考答案摘要
答案 在题目中没有提供答案,因此这里给出一个通用回答:在 JUnit 4 可以使用 @Test(expected=SomeException.class) 或 try/catch + fail();在 JUnit 5 推荐使用 Assertions.assertThrows(SomeException.class, () -> methodCall)。
本题提供 STAR 原则详细解答和技术解析,登录匠人学院学习中心即可查看完整答案。