[英]How to write a JUnit unit test for database call?
例如,如果我有这样的事情:
try {
// db call
} catch ( Exception e ) {
return false
}
我想编写一个单元测试来测试此方法。 我该怎么写? 或者换句话说,将导致引发SQLException或任何其他异常的代码示例是什么?
我们通常编写这样的测试代码:
try {
// Do something you expect to fail.
Assert.fail();
} catch (SQLException e) { // Expected exception type.
Assert.assertEquals(e.getMessage(), "Expected message");
// Or alternatively.
Assert.assertTrue(e.getMessage().startsWith("Expected start of the message"));
}
Assert.fail()部分确保在代码未引发错误的情况下测试失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.