測試預期的 Exception 行為
當某個方法我們預期會丟出 Exception 時,
可利用 fail 這個述詞來測試。
如以下的 JUnit 語法:
可利用 fail 這個述詞來測試。
如以下的 JUnit 語法:
public void testGetUserException() {
User u = null;
try {
u = UserFetcher.getUser("ThisUserIdDoesNotExist");
fail("Should have gotten NoSuchUserException - used a bad user ID");
} catch (NoSuchUserException expected) {
; // Expected - intentional
}
}
留言