Is it possible to change the assert behavior as per the parameters of the @Parameters method.
Class abcTest
{
..
@Parameters
public static Collection<Object[]> testParameters()
{
return Arrays.asList(new Object[][] {
{1},{2}
});
}
..
@Test
public void test()
{
...
if(num == 1) { assertTrue(..); }
if(num == 2) { assertFalse(..); }
...
}
}
is it possible to define the assert behavior exactly the way we define parameters?
Thanks in advance.
In the simplest case you can pass expected values as parameters and use them in assertions, as shown in javadoc.
In more complex cases you need to encapsulate assert logic into objects and pass them as parameters.
If you need different assertions for the same values you can use
assertThat()andMatcher<T>:Otherwise, if different parameters need completely different assertions you can pass them as
Runnables.However, it may be not a good idea to use parameterized tests in this case – if you need completely different assertions for different cases it can be more elegant to create separate test methods for these cases, extracting their commons parts into helper methods: