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