I have the following JUnit test. The method I'm testing is pretty simple, it just gets a number and returns a list with its divisors. I donβt want to repeat the test code many times, so I created a helper method, testDivisorsAux :
@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)); } }
Everything works fine, I'm just wondering ... Is this a bad practice? Should I write the test differently? Perhaps save all the code in the @Test method?
PMD tells me that JUnit tests should include assert () or fail () (for the first method), and JUnit 4 tests that run tests should use the @Test annotation (for the second). I know that PMD only uses regex (well, it's actually XPath) to determine which rules I'm breaking ... so I tend to think that this is just a βfalse positiveβ warning. But in any case, I would like to know that I am doing something wrong. (Appart from writing tests is 4 times longer than the test method :)
While I was looking for questions like this, I found something called parameterized tests ... but it looks like it is something focused on much larger scenarios, right?
junit pmd
Ajperez
source share