Sometimes it makes sense to have more than one assertion in a unit test method, e.g.
@Test
public void testMapReduceProcessing() {
Tuple tuple = new MetadataTuple();
Processor instance = new Processor();
instance.process(tuple);
assertEquals(State.PROCESSED, tuple.getState());
assertEquals(1, tuple.getProcessCount());
}
PMD and possibly other static analysis tools which I might introduce in the future and want to consider during programming now would raise a JUnitTestContainsTooManyAsserts
warning because more than one assert[...]
is present. I could:
- split the methods, but that seems overkill and the recommendation to have only one assert per unit test method doesn't apply here because there's no advantage in splitting here.
assertTrue(a.equals(b) && c.equals(d))
which would complicate the assert failure message and also be bad style becauseequals
should useassertEquals
.- accept that there's no other solution and suppress the PMD check.
I'm not happy with all of them. Is there any elegant approach? I'm looking for JUnit 5 approaches and don't mind extra dependencies.