Citrus-framework-单独类中的DataProvider

时间:2019-03-07 13:51:52

标签: testng testng-dataprovider data-driven-tests citrus-framework

我可以在单独的类中创建数据提供者,并在带有@Factory批注的测试类中使用它吗?

例如:

数据提供程序类:

public class DataProvider {
    private static ServiceBase<SomeService> service;

    @Autowired
    public void setService(ServiceBase<ApiTestHttpRequest> service) {
        DataProvider.service = service;
    }

    private static Object[][] data = null;
    public static final String NAME = "testData";

    @org.testng.annotations.DataProvider(name = NAME, parallel = true)
    public static Object[][] loadDataToTest() {
        if (data == null) {
            ...
        }
        return data;
    }
}

测试类:

public class SomeTests extends TestNGCitrusTestRunner {
    TestData testData;

    @Factory(dataProvider = DataProvider.NAME, dataProviderClass = DataProvider.class)
    public SomeTests(TestData testData){
        this.testData = testData;
    }
    ...
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我通过以下方式解决了这个问题:

public class DataProvider {

private static Object[][] data = null;
public static final String NAME = "testData";

@org.testng.annotations.DataProvider(name = NAME, parallel = true)
public static Object[][] loadDataToTest() {
    if (data == null) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("citrus-context.xml");
        ServiceBase<SomeService> service = (ServiceBase<SomeService>)applicationContext.getBean(*nameOfTheBean*);
        List<SomeService> testData = (List<ApiTestHttpRequest>) service.getTestData();
        ...
    }
    return data;
}

}