我有一个简单的Java
方法,我想检查它是否不会抛出任何异常
。
我已经模拟了参数等,但是我不确定如何使用 Mockito
来测试该方法没有抛出异常?
当前测试代码:
@Test
public void testGetBalanceForPerson() {
//creating mock person
Person person1 = mock(Person.class);
when(person1.getId()).thenReturn("mockedId");
//calling method under test
myClass.getBalanceForPerson(person1);
//How to check that an exception isn't thrown?
}
最佳答案
如果捕获到异常,则测试失败。
@Test
public void testGetBalanceForPerson() {
// creating mock person
Person person1 = mock(Person.class);
when(person1.getId()).thenReturn("mockedId");
// calling method under test
try {
myClass.getBalanceForPerson(person1);
} catch(Exception e) {
fail("Should not have thrown any exception");
}
}
关于java - 如何使用 mockito 检查是否没有抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38868620/