citrusframework / citrus

Framework for automated integration tests with focus on messaging integration
https://citrusframework.org
Apache License 2.0
457 stars 134 forks source link

TestNG DataProvider and CitrusResource injection of TestRunner not working properly #83

Closed ceazy79 closed 8 years ago

ceazy79 commented 8 years ago

Scenario:

Trying to use TestNG DataProvider together with TestRunner resource injection in order to use the runner in a thread-safe way.

Testcase:

`public class DataProviderTest extends TestNGCitrusTest {

private static final Logger log = LoggerFactory.getLogger(DataProviderTest.class);

@DataProvider(name = "getTestData")
public Object[][] getTestData() {
    String[][] result = new String[][] { { "hello" } };
    return result;
}

@Test(testName = "dataProviderAndCitrusResourceInjectionTest",
        enabled = true,
        dataProvider = "getTestData")
@CitrusTest(name = "dataProviderAndCitrusResourceInjectionTest")
@CitrusParameters({"dataRecord" })
@Parameters({"dataRecord", "runner" })
public void dataProviderAndCitrusResource(String dataRecord, @Optional @CitrusResource TestRunner runner) throws Exception {
    runner.echo("Hello Citrus Test World.");
    log.info("DataProvider dataRecord = " + dataRecord);
}

} `

Expected result:

TestRunner executes echo action and log contains string "DataProvider dataRecord = hello"

Actual result:

org.testng.TestNGException: The data provider is trying to pass 1 parameters but the method de.finkonsens.geco.citrus.DataProviderTest#dataProviderAndCitrusResource takes 2 and TestNG is unable in inject a suitable object

at org.testng.internal.Invoker.injectParameters(Invoker.java:1253)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1122)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 at org.testng.internal.Invoker.injectParameters(Invoker.java:1249) ... 21 more

Kind regards, Chris

christophd commented 8 years ago

Fixed it and you should be able to use it like this:

Testcase:

public class DataProviderTest extends TestNGCitrusTest {

    private static final Logger log = LoggerFactory.getLogger(DataProviderTest.class);

    @DataProvider(name = "getTestData")
    public Object[][] getTestData() {
        String[][] result = new String[][] { { "hello", null } };
        return result;
    }

    @Test(testName = "dataProviderAndCitrusResourceInjectionTest",
        enabled = true,
        dataProvider = "getTestData")
    @CitrusTest(name = "dataProviderAndCitrusResourceInjectionTest")
    @Parameters({"dataRecord", "runner" })
    public void dataProviderAndCitrusResource(String dataRecord, @CitrusResource TestRunner runner) throws Exception {
        runner.echo("Hello Citrus Test World.");
        log.info("DataProvider dataRecord = " + dataRecord);
    }
}