JUnit Test Suite: Way to create a dataset first before tests start running
I want to setup data for my entire test suite before any of the tests start running. I understand maven runs the test one by one and not a suite, so I cannot use @SuiteClasses. Also I dont want to create the dataset through dbunit-maven-plugin, the dataset has to be created over REST. Is there a way where I can run specific classes as part of maven pre-integration-test and post-integration-test to setup and clean?
开发者_运维技巧For example
public class TestInit
{
public void setUp()
{
//Data setup
}
public void tearDown()
{
//Data clean up
}
}
make setup run before test suite starts and tearDown after it ends. Or can I run 2 separate classes like, TestInitSetup and TestInitTearDown?
Here is a Rule based solution. It may be useful.
The syntax looks like this:
public class SimpleWayToUseDataSetTest {
@Rule
public DataSetRule rule = new DataSetRule(); // <-- this is used to access to the testVectors from inside the tests
public static class MyDataSet extends SimpleTestVectors {
@Override
protected Object[][] generateTestVectors() {
return new Object[][] {
{true, "alpha", new CustomProductionClass()}, // <-- this is a testVector
{true, "bravo", new CustomProductionClass()},
{false, "alpha", new CustomProductionClass()},
{false, "bravo", new CustomProductionClass() }
};
}
}
@Test
@DataSet(testData = MyDataSet.class) // <-- annotate the test with the dataset
public void testFirst() throws InvalidDataSetException { // <-- any access to testData may result in Exception
boolean myTextFixture = rule.getBoolean(0); // <-- this is how you access an element of the testVector. Indexing starts with 0
String myAssertMessage = rule.getString(1); // <-- there are a couple of typed parameter getters
CustomProductionClass myCustomObject = (CustomProductionClass) rule.getParameter(2); // <-- for other classes you need to cast
Assert.assertTrue(myAssertMessage, true);
}
}
If you can't find a solution in JUnit, TestNG supports @BeforeSuite and @AfterSuite, which seem to do what you want.
精彩评论