10

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?

budi
  • 6,351
  • 10
  • 55
  • 80
  • I don't understand. Why can't you just add `WhitespaceTest` to your current suite? Also of course you have have multiple suites – dkatzel Oct 29 '15 at 20:08
  • I want to run each test in `WhitespaceTest` for each `BaseTest` in the current suite. – budi Oct 29 '15 at 20:13
  • Sounds more like a multiple inherrtance problem than a suite problem – dkatzel Oct 29 '15 at 20:15
  • I could have `SomeTest` inherit `WhitespaceTest` and all tests in both `BaseTest` and `WhitespaceTest` will run. But what if I have something like `DashTest`? I can't have `SomeTest` inherit both `WhitespaceTest` and `DashTest` obviously, so what then? So yes, I agree it may be an inheritance problem as well. – budi Oct 29 '15 at 20:25
  • Sound for me that you have two dimensions of testing: Sets of parameters (SomeTest, AnotherTest), Sets of properties to be tested (BaseTest, WhitespaceTest, DashTest). Based on this I would recommend to put the property tests into the TestSuite, injecting the set of parameters into each. The computation of parameters could be part of another class that manages several subsets (e.g. Some, Another, ...). – CoronA Oct 31 '15 at 18:16
  • @CoronA Good ideas here. One question, does this mean I have to essentially merge all parameters into one collection? – budi Oct 31 '15 at 21:22
  • That depends on your objectives. Providing all parameters from one class is probably the easiest way. Another way could be to group similar parameters in their own classes and have a facade class that merges all of them at runtime. – CoronA Nov 01 '15 at 00:25
  • Yet your objectives are not clear to me, so I cannot propose a solution. Maybe you want to find a solution on your own. Then perhaps switch from Junit Parameters to [Junit Theories](https://github.com/junit-team/junit/wiki/Theories) would help. – CoronA Nov 06 '15 at 21:07

1 Answers1

1

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.

budi
  • 6,351
  • 10
  • 55
  • 80
  • I think that cannot work: You run your BaseTest with a set of parameters (expected, actual). Testing Whitespaces would afford another set of parameters than Testing Null behaviour. Perhaps this points to a design failure of your BaseTest class (it just discards expected/actual). – CoronA Nov 06 '15 at 20:37
  • It works, it's just a level of indirection that I don't want. But yes, I am currently redesigning my entire testing module to what you had recommended earlier in the week. – budi Nov 06 '15 at 21:07