Citrus-framework - DataProvider在一个单独的类中

问题描述 投票:0回答:1

我可以在单独的类中创建数据提供程序,并在带有@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;
    }
    ...
}
testng testng-dataprovider data-driven-tests citrus-framework
1个回答
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;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.