Java JUnit测试套件:在测试开始运行之前首先创建数据集的方法
我想在任何测试开始运行之前为我的整个测试套件设置数据。我知道maven一个接一个地运行测试,而不是一个套件,所以我不能使用@SuiteClasses。另外,我不想通过DbUnitMaven插件创建数据集,数据集必须在REST上创建。有没有一种方法可以让我运行特定的类作为maven集成前测试和集成后测试的一部分来进行设置和清理 比如说Java JUnit测试套件:在测试开始运行之前首先创建数据集的方法,java,maven,junit,Java,Maven,Junit,我想在任何测试开始运行之前为我的整个测试套件设置数据。我知道maven一个接一个地运行测试,而不是一个套件,所以我不能使用@SuiteClasses。另外,我不想通过DbUnitMaven插件创建数据集,数据集必须在REST上创建。有没有一种方法可以让我运行特定的类作为maven集成前测试和集成后测试的一部分来进行设置和清理 比如说 public class TestInit { public void setUp() { //Data setup }
public class TestInit
{
public void setUp()
{
//Data setup
}
public void tearDown()
{
//Data clean up
}
}
使安装程序在测试套件启动前运行,并在测试套件结束后拆除。或者我可以运行两个单独的类,比如TestInitSetup和TestInitTearDown吗?如果在JUnit中找不到解决方案,TestNG支持@BeforeSuite和@AfterSuite,这两个类似乎可以满足您的需要。如果在JUnit中找不到解决方案,TestNG支持@BeforeSuite和@AfterSuite,这似乎是你想要的。是一个基于规则的解决方案。这可能有用 语法如下所示:
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);
}
}
公共类SimpleWayToUseDataSetTest{
@统治
public DataSetRule rule=new DataSetRule();//是一种基于规则的解决方案。它可能很有用
语法如下所示:
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);
}
}
公共类SimpleWayToUseDataSetTest{
@统治
公共数据集规则=新数据集();//你为什么不想使用dbunit,你能给我一些解释吗?我有很多数据要播种,通过提供一个xml数据集这样做很麻烦。我有一个REST资源端点,它接受一个相当简单的json负载并将数据插入数据库。这只是一个方便的问题。你为什么不想使用dbunit,可以吗我可以解释一下吗?我有很多数据要播种,通过提供xml数据集来播种很麻烦。我有REST资源端点,它接受相当简单的json负载并将数据插入数据库。这只是一个方便的问题。