Fluent assertions
First of all, use FEST-Assertions library to introduce pleasantly looking assertions with meaningful error messages:
assertThat(method(arg1)).containsExactly(a, b, c);
assertThat(method(arg2)).containsExactly(a, b, c);
assertThat(method(arg3)).containsExactly(a, b, c);
The BDD way
But I understand your question is not about the syntax, but about methodology: what should you do if arg4
needs to be tested? Well, if arg1
through arg4
have a different semantic meaning, I would advice you to have a separate test for each argument. Very verbose, but also extremely readable (pseudocode):
@Test
public void shouldReturnAbcWhenSomeArgumentUsed() {
//given
Object arg = arg1;
//when
Set<Object> result = method(arg);
//then
assertThat(result).containsExactly(a, b, c);
}
..and repeat this for every test. The key part is: method name should represent the meaning of an argument, what does this method actually test, what do you expect, what is the scenario?
Consider testing isEven
method. I would recommend the following tests:
shouldReturnTrueForZero
shouldReturnTrueForSmallPositiveEvenNumber
shouldReturnTrueForLargePositiveEvenNumber
shouldReturnFalseForSmallPositiveOddNumber
shouldReturnFalseForLargePositiveOddNumber
- ... and mirror the tests for negative numbers
Each test represent a slightly different, well defined scenario. On the other hand you might generate literally thousands of shouldReturnFalseWhen227
, but what is the value of such a test suite, except it's large? Test semantically different arguments and corner cases, defining precisesly what case is being tested.
Parameterized way
If you really want to have just a single generic test method, Parameterized runner is the way to go. I think the example is self-explanatory. NB: you can parameterize expected values as well.
@RunWith(value = Parameterized.class)
public class JunitTest6 {
private Object arg;
public JunitTest6(Object arg) {
this.arg = arg;
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][]{
{arg1},
{arg2},
{arg3}
});
}
@Test
public void testMethod() {
assertThat(method(arg)).containsExcatly(a, b, c);
}
}
Based on this.