JUnit 5
I have a lot of number properties in separate classes. They all implement Property interface and I would like to test them all. All test looks identical like this:
@DisplayName("Given Jumping property")
class JumpingTest {
private Property underTest;
@BeforeEach
void setUp() {
underTest = new Jumping();
}
@DisplayName("given test() method")
@ParameterizedTest(name = "when number is \"{0}\" then \"{1}\"")
@CsvFileSource(resources = "/property/jumping.csv", numLinesToSkip = 1)
void testProperty(final BigInteger number, final boolean expected) {
assertEquals(expected, underTest.test(number));
}
}
The other property and test class for comparing is
@DisplayName("Given Buzz property")
class BuzzTest {
private Property underTest;
@BeforeEach
void setUp() {
underTest = new Buzz();
}
@DisplayName("given test() method")
@ParameterizedTest(name = "when number is \"{0}\" then \"{1}\"")
@CsvFileSource(resources = "/property/buzz.csv", numLinesToSkip = 1)
void testProperty(final BigInteger number, final boolean expected) {
assertEquals(expected, underTest.test(number));
}
}
As you can see the class for Property underTest is changed. Other changes are in @CsvSourceFile and in the description of the test class. These strings can be calculated by this.getClass().getSimpleName(). But I do not know how to substitute this strings.
How I can remove the code duplication?
Is it possible to eliminate a duplicate code in JUnit 5?
How to properly do this?