Hi,
I have the following JUnit test:
@Test
public final void testDivisors() {
testDivisorsAux(1, new int[] { 1 });
testDivisorsAux(6, new int[] { 1, 2, 3, 6 });
testDivisorsAux(0, new int[] { });
...
}
private final void testDivisorsAux(final int number, final int[] expected) {
List<Integer> divisors = Util.divisors(number);
assertSame(divisors.size(), expected.length);
for (int i : expected) {
assertTrue(divisors.contains(i));
}
}
The method that I'm testing is quite simple, it just receives a number and returns a List with its divisors. I don't want to repeat the test code many times, so I've created an auxiliar method, testDivisorsAux
.
Everything works fine, I'm just wondering... is this a bad practice? Should I write the test in a different way? Maybe keep all the code within the "@Test
method"?
PMD is telling me that JUnit tests should include assert() or fail()
(for the first method), and JUnit 4 tests that execute tests should use the @Test annotation
(for the second one). I know that PMD is using just a regular expression (well, actually it's XPath) to determine which rules I'm breaking... so I'm inclined to think that it's simply a "false positive" warning. But anyway, I would like to know if I'm doing something wrong. (Appart from writing tests 4 times longer than the method being tested :)
While I was searching for questions similar to this one, I've found something called parametrized tests... but it seems it's something oriented to much bigger scenarios, isn't it?