I want to create a test suite builder for some common use cases in our company (like Google Guave does for collections).
So I create my builder, and some classes extending either TestSuite
or TestCase
that get added if needed. However running everything in Eclipse (Mars 4.5.1 - don't judge) shows the tests as "Unrooted Tests" and the view is not able to mark them as either successful nor failed:
Maven brings the following exception (but it's Maven, I can burn that bridge when I get to it, I'm not convinced it has anything to do with the above):
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project org.acme.project Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: There was an error in the forked process
Other questions state this is due to using JUnit 4 for the JUnit 3 class TestCase
(which is not deprecated, so...), in which case the question would be: How do I convert this code to JUnit 4?
@RunWith(Suite.class)
@Suite.SuiteClasses({ MyTest.Suite.class })
public class MyTest {
public static class Suite {
public static TestSuite suite() {
final TestSuite suite = new TestSuite("My Test Suite");
suite.addTest(new CompoundTester(value -> new MyTester(value), "A", "B"));
return suite;
}
}
public static class CompoundTester extends TestSuite {
public CompoundTester(final Function<String, TestCase> testFactory, final String... values) {
String name = "unknown";
for (final String value : values) {
final TestCase testCase = testFactory.apply(value);
name = testCase.getName();
addTest(new TestCase(value.toString()) {
@Override
public void run(final TestResult result) {
testCase.run(result);
}
});
}
setName(name);
}
}
public static class MyTester extends TestCase {
private final String object;
public MyTester(final String object) {
super("testSomething");
this.object = object;
}
public void testSomething() {
Assert.assertNotNull(this.object);
}
}
}
It works if the run
method of the anonymous TestCase
class looks like this:
@Override
public final void run(final TestResult result) {
result.startTest(this);
result.runProtected(this, () -> testCase.run());
result.endTest(this);
}
No idea why, though.