In JUnit 4, I am looking to write a test suite that is made up of multiple flavors of the same test case, just with different initial conditions on each one. Here is an example:
import java.io.File;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({MultiInputClientServerIntegrationTest.NormalInput.class,
MultiInputClientServerIntegrationTest.SimulationHashIssue.class})
public class MultiInputClientServerIntegrationTest {
@RunWith(Suite.class)
@SuiteClasses({TestClientServerIntegration.class})
public class NormalInput {}
@RunWith(Suite.class)
@SuiteClasses({TestClientServerIntegration.class})
public class SimulationHashIssue {
public SimulationHashIssue() {
TestClientServerIntegration.simulation = new File("test\\BEECHA01\\sim2.zip");
TestClientServerIntegration.inputFile = "files\\config.in";
}
}
}
As you can see, both inner classes have SuiteClasses of the TestClientServerIntegration.class
but the second one is changing some static variable values. I am finding that this constructor never gets called, so these statics never get changed.
My end goal is to run this TestClientServerIntegration.class
over and over with multiple types of input. If I can run a test suite this way, that would be ideal -- so hopefully it is possible. I'd like to do as little hacking of JUnit as possible, but what needs to get done will get done.
Oh, keep it simple! Your test class can have
I solved it! The book JUnit in action helped a lot. Here is my code:
Where the special Runners are:
And,
What helped a lot was actually switching to JUnit 4.10 as it gave a more detailed error message. Anyway the main differences here is that I am having my "custom input" tests extend the actual test. Then I created an interceptor which gets overloads the @Before and @After methods and can alter parameters before each individual @Test.
In truth, I'd prefer something that just overloaded each @BeforeClass but beggars aren't choosers. This is good enough and does the job right. It works with Eclipse. Hopefully I'll run across a hook for @BeforeClass and work that instead.
What about an alternative solution?
1.Use template pattern to extract an abstract test class, and make the initial condition preparement an abstract method.
2.Each test case extends the template and override the implentation of initial condition preparement.
3.Group them all in a test suite.