1
我想測試我的contentprovider的getType方法,特別是當傳入無效的uri時。目前我拋出IllegalArgumentException,但在我的測試中沒有發現異常。如何測試Android ContentProvider中引發的異常
這裏是我的測試
public void testIllegalArgumentExceptionThrownForInvalidUri() {
Uri badUri = Uri.parse("content://" + NoteProvider.AUTHORITY + "/somethingelse");
try {
String type = mockResolver.getType(badUri);
fail("No exception thrown");
} catch (Exception ex) {
assertEquals(IllegalArgumentException.class, ex.getCause().getClass());
}
}
和
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case URI_NOTES:
return Notes.CONTENT_TYPE;
case URI_NOTE_ID:
return Notes.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
我已經通過它加強,可以看到,拋出異常的getType的實施,但在測試中失敗()方法被調用而不是catch塊。
MockContentResolver是否吞下異常?
聲明:我是新來的android和java,雖然已經在C#編碼了好幾年了,所以我可能會做一些完全愚蠢的事情。 :D