的方法的assertThat
簽名是:
<T> void assertThat(T actual, Matcher<? super T> matcher)
其中:
actual
爲被比較
matcher
是一個表達式,內置的{@link匹配器} S,指定允許值
所以所計算的值,其意在讀類似:
assert that this computed value matches that expected value
鑑於此,在你的問題中斷言:
Set<String> result = program.getCollection("inputdata");
assertThat(3, is(result.size()));
應該表示爲:
assertThat(result.size(), is(3));
這是一個微妙的差異,但它是更符合assertThat
簽名和Hamcrest匹配器的規範是一致的。
問題的其餘部分處理您是否應該使用is
或iterableWithSize
。以下所有斷言都是(a)在功能上等同的,以及(b)非常暢銷的(恕我直言)。
assertThat(result.size(), is(3));
assertThat(result, iterableOfSize(3));
assertThat(result, hasSize(3));
沒有任何令人信服的理由(除主觀意見之外)選擇其中任何一個。我建議選擇一種方法並持續使用,因爲這裏的「一致性原則」應該優於正確性,因爲它們都是〜同樣正確的。
gr8,回答我的問題。 +1。正確的hamcrest提供了更可讀的風格。 – samshers