Exception 异常的Junit测试
我尝试测试我的异常JUnit,但测试未通过我有以下错误跟踪:Exception 异常的Junit测试,exception,junit4,Exception,Junit4,我尝试测试我的异常JUnit,但测试未通过我有以下错误跟踪: org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnitAndHigherRunnerImpl.java:37) 及 这是我的代码: PatientEntityFacade pef = new PatientEntityFacade(); Mockito.when(pef.findByNumber(5555)).thenReturn(
org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnitAndHigherRunnerImpl.java:37)
及
这是我的代码:
PatientEntityFacade pef = new PatientEntityFacade();
Mockito.when(pef.findByNumber(5555)).thenReturn(patientEntity);
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldThrow() throws PatientNotFoundException
{
thrown.expect(PatientNotFoundException.class);
thrown.expectMessage("personalized exception no patient found");
try {
pef.findByNumber(5555);
} catch (com.patient.facade.PatientNotFoundException e) {
e.printStackTrace();
}
}
如果你想测试你的
异常
,那就用正确的方法去做
定义何时应引发异常
- 在
中,如果每个方法@BeforeClass
- 在
-方法中,如果只有此方法应该抛出它@Test
any(X.class)
不要在单元测试中尝试catch。
以这种方式捕获,如果没有异常,测试将失败
@Test(expected = PatientNotFoundException.class)
public void shouldThrow()
pef.findByNumber(5555);
}
你能提供属于那个错误的代码吗?也许你的构建脚本的一部分和使用过的IDE?检查我上一次的编辑我已经提醒过应该在@Test method中使用它,我试图避免try-catch,但@LenglBoy仍然不起作用
@Test(expected = PatientNotFoundException.class)
public void shouldThrow()
pef.findByNumber(5555);
}