Search code examples
testngtestng-dataproviderdata-driven-testscitrus-framework

Citrus-framework - DataProvider in a separate class


Can I create data provider in a separate class and the use it in a test class with @Factory annotation?

For example:

Data provider class:

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

Test class:

public class SomeTests extends TestNGCitrusTestRunner {
    TestData testData;

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

Solution

  • I found the solution. I solved this in following way:

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

    }