参见英文答案 >
How do you assert that a certain exception is thrown in JUnit 4 tests?32个
我正在使用JUnit 4.11.
我正在使用JUnit 4.11.
Eclipse版本:Luna Service Release 2(4.4.2)
我需要做异常测试.代码可能是这样的:
- // src code
- public void myMethod throws MyException {
- ...
- throw new MyException(msg);
- ...
- }
- // test code
- @Test (expected = MyException.class)
- public void testMyMethod() {
- try {
- myMethod();
- fail();
- } catch (MyException e) {
- assertEquals(expectedStr,e.getMessage());
- }
- }
但测试总是失败,我错在哪里?