2011-04-09 27 views
1

我想在任何测试开始运行之前为我的整个测试套件设置数据。我明白Maven一个接一个地运行测试而不是套件,所以我不能使用@SuiteClasses。另外我不想通过dbunit-maven-plugin创建数据集,数据集必须通过REST创建。有没有一种方法可以将特定的类作为Maven预集成测试和后整合测试的一部分来安装和清理?之前的测试套件开始和结束后即拆除JUnit Test Suite:在测试开始运行之前首先创建数据集的方法

例如

public class TestInit 
{ 
    public void setUp() 
    { 
     //Data setup 
    } 

    public void tearDown() 
    { 
     //Data clean up 
    } 
} 

使安装运行。或者我可以运行2个独立的类,如TestInitSetup和TestInitTearDown?

+0

为什么你不想使用DbUnit,你可以给我一些解释? – 2011-04-10 00:22:24

+0

我有很多数据需要种子,通过提供一个xml数据集很麻烦。我有REST资源端点,它接受一个相当简单的json负载并将数据插入到数据库中。这只是一个方便的问题。 – Prasanna 2011-04-10 22:20:54

回答

1

如果您无法在JUnit中找到解决方案,TestNG支持@BeforeSuite和@AfterSuite,这似乎是您想要的。

4

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); 
    } 
} 
相关问题