In a school assignment, I should write blackbox test for a method that returns true
for a parameter < 80 and false
otherwise. Currently my approach would be
for (int i = 0; i < 80; i++) {
assertTrue(someMethod(i));
}
for (int i = 80; i <= 100; i++) {
assertFalse(someMethod(i));
}
However, this would require 100 seperate assertions. Is there a best/better practice method? If relevant, I'm using JUnit 5 but could switch to JUnit 4 if required (it's just a school assignment after all). Regards.
For JUnit 5 consider the repeated test feature:
https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests
For your case you could repeat 100 times:
@ParameterizedTest
@MethodSource("args")
public void so(int argument)
{
assertTrue((argument < 80) == someMethod(argument));
}
private static IntStream args()
{
return IntStream.range(0, 100);
}
I would agree checking 1 - 100 is not totally necessary, just check around 80 (79, 80, 81). The only other case that might be worthwhile is checking a negative number but even that seems overkill in this case.
If you do decide to just check 79,80,81 then the ValueSource
annotation is cleaner:
@ParameterizedTest
@ValueSource(ints = { 79, 80, 81 })
public void so(int argument)
{
assertTrue((argument < 80) == someMethod(argument));
}