2016-06-12 96 views
1

我可以很容易做到的說法有兩個可能的結果:Hamcrest斷言

assertThat(result, anyOf(true, false)); // just a sample, doesn't make sense as an assertion 

不過,我需要執行斷言,我的結果之一是等於某個值:

assertThat(result1 || result2, is(true)); 

上述工作,但錯誤信息並沒有說哪個結果是false。在Hamcrest有什麼類似於下面的內容嗎?

assertThat(anyOf(result1, result2), is(true)); // just a hypothetical assertion 
+2

assertTrue(result1 || result2)有什麼問題。如果斷言失敗,那麼result1和result2都是false。你不需要任何特別的推斷。 –

回答

4

你可以寫在反向斷言:

assertThat(true, anyOf(is(result1), is(result2))) 

這仍然會拋出斷言錯誤時,無論是result1result2true,消息會告訴雙方最終值或result1result2 ...在預期的部分,這使得它有點尷尬。


從你的問題:

不過,我需要執行斷言,我的結果之一是等於某個值:

這意味着你的實際使用情況是確定您的結果列表是否具有給定值。這可以清楚地表示爲:

assertThat(Arrays.asList(result1, result2), hasItem(true)); 

這聲明由兩個結果形成的列表具有給定的項目。如果不是,則斷言錯誤將爲:

Expected: a collection containing <true> 
    but: was <false>, was <false> 

該消息告訴您集合中每個元素的值。