nicholas
nicholas

Reputation: 2762

Cucumber-jvm: Cannot inject @Test with parameters CucumberFeatureWrapper

I running a project using BDD Cucumber and TestNG but it encounter error at below.

Cannot inject @Test annotated Method [feature] with [interface cucumber.api.testng.CucumberFeatureWrapper].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection

I know that @Test method can only have @ITestContext annotation. How to run my feature file?

@CucumberOptions(
        features = "src/Feature",
        glue = {"Step_Definitions"},
        plugin = {
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/CucumberTestReport.json",
                "rerun:target/cucumber-reports/rerun.txt"
})
public class TestRunner {
    private TestNGCucumberRunner testNGCucumberRunner;
    private ExtentReportManager reportMgr;
    private PropertiesManager pm;

    @BeforeTest(alwaysRun = true)
    public void setUp() throws Exception {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());

        pm = PropertiesManager.createInstance();
        reportMgr = ExtentReportManager.createInstance();

        WebDriverManager.createDriver();
    }

    @Test(groups = "cucumber", description = "Runs Cucumber Feature"/*, dataProvider = "dataProvider"*/)
    public void feature(CucumberFeatureWrapper cucumberFeature) {
        //ITestContext context
        testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
    }



    /*@DataProvider(name = "dataProvider", parallel = true)
    public Object[][] features() {
        return testNGCucumberRunner.provideFeatures();
    }*/

    @AfterTest(alwaysRun = true)
    public void tearDown() throws Exception {
        reportMgr.getExtent().flush();
        testNGCucumberRunner.finish();
    }

I tried this tutorial but cannot import RunCukesStrict.class I using version 2.4.0 for cucumber-jvm. Any concrete examples?

Upvotes: 1

Views: 1229

Answers (1)

M.P. Korstanje
M.P. Korstanje

Reputation: 12039

The tutorial you're linking to is not a tutorial. It's the unit test of the TestNGCucumberRunner. Try the example section instead.

Cucumber expects you to inject the individual scenarios rather then the whole test runner like so:

@Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")
public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper cucumberFeature) throws Throwable {
    testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
}

@DataProvider
public Object[][] scenarios() {
    return testNGCucumberRunner.provideScenarios();
}

Upvotes: 3

Related Questions