有没有办法构建一个组合的 Hamcrest 匹配器来测试一个对象和这个对象的属性?- 伪代码:
both(
instanceof(MultipleFailureException.class)
).and(
// pseudo code starts
adapt(
new Adapter<MultipleFailureException, Iterable<Throwable>()
{
public Iterable<Throwable> getAdapter(MultipleFailureException item)
{
return item.getFailures();
}
},
// pseudo code ends
everyItem(instanceOf(IllegalArgumentException.class))
)
)
背景:我有一个 JUnit 测试,它遍历动态对象的集合。每个对象在处理时都应抛出异常。收集异常。测试应该以包含这些抛出异常的集合的 MultipleFailureException 结束:
protected final ExpectedException expectation = ExpectedException.none();
protected final ErrorCollector collector = new ErrorCollector();
@Rule
public RuleChain exceptionRules = RuleChain.outerRule(expectation).around(collector);
@Test
public void testIllegalEnumConstant()
{
expectation.expect(/* pseudo code from above */);
for (Object object : ILLEGAL_OBJECTS)
{
try
{
object.processWithThrow();
}
catch (Throwable T)
{
collector.addError(T);
}
}
}