2014-12-04 69 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))))) 
     ); 

但是,让我说,我不能想象一个测试中,你不知道的标志值是真还是假。如果两种情况都可能发生,则必须编写两个测试。