2014-12-04 63 views
1

我有一個參數化測試,其中一個參數是布爾值,以顯示測試是否通過或失敗。Hamcrest條件匹配器?

在我的測試,我有以下幾點:

的參數是這樣的:{0, 1, true}

的測試方法包括:

 
if (expectedResult) { 
    assertThat(myList, matchesUsingMyCustomMatcher(otherList)); 
} else { 
    assertThat(myList, not(matchesUsingMyCustomMatcher(otherList.subList(start, end)))); 
} 

什麼,我不想做是寫一個if-else語句。因此我的問題是:是否有條件匹配器?例如: assertThat(myList, ifMatcher(expected, matchesUsingMyCustomMatcher(otherList)));

+0

不知道我對你的理解是否正確,但是......在數學上,「如果一個接下來的b」等價於「(不是a)或b」,那麼它將使用'anyOf'與'not 「匹配者? – ajb 2014-12-04 17:26:15

+0

在http://hamcrest.org/JavaHamcrest/javadoc/1.3/中描述了一個'Condition'類。也許這就是你需要的? – ajb 2014-12-04 17:27:30

+0

@ajb謝謝你,我會看看條件。此外,我編輯我的帖子,使其更清楚我想實現什麼。 – Ibolit 2014-12-04 17:40:25

回答

1

正如你可以從源代碼檢查,Condition不會做的伎倆。你可以用邏輯條件發揮如下:

assertThat(myList, 
    either(allOf(is(expectedResult), matchesUsingMyCustomMatcher(otherList))) 
    .or(allOf(not(is(expectedResult)), not(matchesUsingMyCustomMatcher(otherList.subList(start, end))))) 
     ); 

但是,讓我說,我不能想象一個測試中,你不知道的標誌值是真還是假。如果兩種情況都可能發生,則必須編寫兩個測試。