I currently have an abstract BaseTest
class which holds several generic @Test
's.
public abstract class BaseTest {
private String expected;
private String actual;
public BaseTest(String expected, String actual) {
this.expected = expected;
this.actual = actual;
}
public String methodToTest(String line) {
return line.trim();
}
@Test
public void testNull() {
assertNull(methodToTest(null));
}
// more @Tests...
}
SomeTest
extends BaseTest
, where I define various test cases.
@RunWith(Parallelized.class)
public class SomeTest extends BaseTest {
// @Parameters (test cases) goes here...
}
I have many tests that extends BaseTest
, which I then put in a Suite
in RunAllTests
.
@RunWith(Suite.class)
@SuiteClasses({ SomeTest.class, AnotherTest.class, etc... })
public class RunAllTests { }
So let's say I want to add more @Test
's, however I want to contain them into a different class.
public class WhitespaceTest extends BaseTest {
public WhitespaceTest(String expected, String actual) {
super(expected, actual);
}
@Test
public void testNewline() {
assertEquals(expected, methodToTest(actual + "\n"));
}
// more @Tests...
}
It appears I need another "layer" of Suites
to run every single test, so that for each class in RunAllTests
, I run the BaseTest
AND WhitespaceTest
. How do I go about implementing this layer?
Here is my current (naive) workaround this problem:
Within BaseTest
:
@Test
public void testNewline() {
WhitespaceTest wt = new WhitespaceTest(expected, actual);
wt.testNewline();
}
This works, but there is a lot of duplicate code. For every @Test
in WhitespaceTest
, you must create another @Test
in BaseTest
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With