Cannot run testng because ngcucumber failed to run

23 Views Asked by At

I have this error:

Caused by: java.lang.NullPointerException: Cannot invoke "io.cucumber.testng.TestNGCucumberRunner.provideScenarios()" because "this.testNGCucumberRunner" is null
    at Runner.TestRunner.scenarios(TestRunner.java:36)

FAILED CONFIGURATION: @AfterClass Runner.TestRunner.tearDownClass
java.lang.NullPointerException: Cannot invoke "io.cucumber.testng.TestNGCucumberRunner.finish()" because "this.testNGCucumberRunner" is null
    at Runner.TestRunner.tearDownClass(TestRunner.java:41)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
    at org.testng.

here's my runner file unable to execute struggling from past 10 ndays, using all the latest dependencies

@CucumberOptions(
        features="src/test/java/features",
        glue="sr//test//java//steps"
      // plugin={"pretty", "io.qameta.allure.cucumber7jvm.AllureCucumber7Jvm"}
      //  tags="@Amazon"
)
public class TestRunner   {
    
    private TestNGCucumberRunner testNGCucumberRunner;

    @BeforeClass(alwaysRun = true)
    public void setUpClass() {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }

    @Test(groups = "cucumber", description = "Runs Cucumber Scenarios", dataProvider = "scenarios")
    public void scenario(PickleWrapper pickle, FeatureWrapper cucumberFeature) {
        testNGCucumberRunner.runScenario(pickle.getPickle());
    }

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

    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        testNGCucumberRunner.finish();
    }

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

Can you help me to fix the code for cucumber with testng?

1

There are 1 best solutions below

0
M.P. Korstanje On

You actually have a few problems that result in a "nothing is working". So fixing one problem, will not immediately get you something that works.

So the first problem you are looking at is that you have an NPE:

java.lang.NullPointerException: Cannot invoke "io.cucumber.testng.TestNGCucumberRunner.finish()" because "this.testNGCucumberRunner" is null
    at Runner.TestRunner.tearDownClass(TestRunner.java:41)

Which means that testNGCucumberRunner here is null (read the stack trace carefully).

    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        testNGCucumberRunner.finish();
    }

Which means that it was never initialized, which means that new TestNGCucumberRunner must have thrown an exception here:

    @BeforeClass(alwaysRun = true)
    public void setUpClass() {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }

You didn't mention any other exceptions, but if you add a null check to the tearDownClass you should see it in the output. And that exception may give you a hint at what your other problems are.

    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        if (testNGCucumberRunner == null) {
            return;
        }
        testNGCucumberRunner.finish();
    }