Here是基于规则的解决方案。它可能是有用的。
的语法如下:
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);
}
}
为什么你不想使用DbUnit,你可以给我一些解释? – 2011-04-10 00:22:24
我有很多数据需要种子,通过提供一个xml数据集很麻烦。我有REST资源端点,它接受一个相当简单的json负载并将数据插入到数据库中。这只是一个方便的问题。 – Prasanna 2011-04-10 22:20:54