Issue
In a jUnit test case, I'm trying to use an Assume that will check if a condition is true OR if another one is true. However, jUnit will effectively stop the test as soon as one condition is true.
In the example listed below, I'm trying to run the test if the reason is null OR if the enumValue is in the allowedReasons EnumSet.
An EnumSet will not allow a null value in its members and I'd like to stick with an EnumSet to mimic what the actual class is using to validate its reason.
@RunWith(Parameterized.class)
public static class AllowedExclusionsTest
{
@Parameters
public static Iterable<EntitlementReason> data()
{
final List<EntitlementReason> data = new ArrayList<>();
data.addAll(Arrays.asList(EntitlementReason.values()));
data.add(null);
return data;
}
@Parameter(0)
public EntitlementReason reason;
private final Set<EntitlementReason> allowedReasons =
EnumSet.of(EntitlementReason.INCARCERATED, EntitlementReason.ABSENT_FROM_CANADA);
@Test
public void testAllowedExclusionReason()
{
Assume.assumeThat(reason, Matchers.isIn(allowedReasons));
Assume.assumeThat(reason, Matchers.nullValue());
final ExcludedPeriodFact test = new ExcludedPeriodFact();
test.setExclusionReason(reason);
Assert.assertEquals("getExclusionReason()", reason, test.getExclusionReason());
}
}
Solution
It turns out that using the Matcher anyOf will do just that.
@Test
public void testAllowedExclusionReason()
{
Assume.assumeThat(reason, Matchers.anyOf(Matchers.nullValue(), Matchers.isIn(allowedReasons)));
final ExcludedPeriodFact test = new ExcludedPeriodFact();
test.setExclusionReason(reason);
Assert.assertEquals("getExclusionReason()", reason, test.getExclusionReason());
}
Answered By - Dark-Wynd