39
使用Java的hamcrest庫,什麼是應該做的對面一個很好的可讀的方式:我要確保someCollection
不包含項目someItem
斷言集合不包含項目
使用Java的hamcrest庫,什麼是應該做的對面一個很好的可讀的方式:我要確保someCollection
不包含項目someItem
斷言集合不包含項目
取反
assertThat(someCollection, hasItem(someItem))
在hasItem
斷言
assertThat(someCollection, not(hasItem(someItem)))
如果您需要斷言一個數組,同樣的邏輯使用 '不(hasItemInArray())'
final String[] availableIds = {"123", "321"};
final String userId = "333";
softAssert.assertThat("Id not found", availableIds, not(hasItemInArray(userId)));
softAssert.assertAll();
一旦我進口包IsNot運算('進口靜態org.hamcrest.core.IsNot.not'),效果不錯。 – harschware 2012-02-20 03:32:00
'Matcherzs'定義了所有的。工廠方法,所以你可以有一個'*'靜態導入。如果沒有其他人在下一個小時內發帖,我會查找一份作品。 – 2012-02-20 17:58:46
@harschware - 從[基礎教程](http://code.google.com/p/hamcrest/wiki/Tutorial),導入靜態org.hamcrest.MatcherAssert.assertThat;和導入靜態org.hamcrest。匹配器。*;' – 2012-02-20 18:19:11