オブジェクトとこのオブジェクトのプロパティをテストする組み合わせ 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);
}
}
}