【问题标题】:JUnit Test Suite: Way to create a dataset first before tests start runningJUnit Test Suite:在测试开始运行之前首先创建数据集的方法
【发布时间】:2011-04-09 19:16:14
【问题描述】:

我想在任何测试开始运行之前为我的整个测试套件设置数据。我知道 maven 一个一个地运行测试而不是一个套件,所以我不能使用@SuiteClasses。另外我不想通过 dbunit-maven-plugin 创建数据集,数据集必须通过 REST 创建。有没有一种方法可以让我在 maven 预集成测试和集成后测试中运行特定类来设置和清理?

例如

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

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

在测试套件启动之前运行安装程序,并在它结束后拆除。或者我可以运行 2 个单独的类,例如 TestInitSetup 和 TestInitTearDown?

【问题讨论】:

  • 为什么不想用dbunit,能给我解释一下吗?
  • 我有很多数据要播种,通过提供 xml 数据集来做这件事很麻烦。我有 REST 资源端点,它接收一个相当简单的 json 有效负载并将数据插入数据库。这只是为了方便。

标签: java maven junit


【解决方案1】:

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);
    }
}

【讨论】:

    【解决方案2】:

    如果你在 JUnit 中找不到解决方案,TestNG 支持@BeforeSuite 和@AfterSuite,它们似乎可以满足你的需求。

    【讨论】:

      猜你喜欢
      • 2010-11-14
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 2017-09-08
      相关资源
      最近更新 更多