cucumber / cucumber-jvm

Cucumber for the JVM
https://cucumber.io
MIT License
2.7k stars 2.02k forks source link

Why did i get “DuplicateStepDefinitionException” on my cucumber test? #1525

Closed samixchoumi closed 5 years ago

samixchoumi commented 5 years ago

I'm making some cucumber steps and i keep having this issues : DuplicateStepDefinitionException

I tried to correct this by my own hand but nothing worked, the question about this exception didn't helped me to correct my issue.

My step in error :

public abstract class FicheSteps<T extends FicheBean, U extends ServiceFicheBO> implements Fr{
    protected T bean;
    protected U service;

    public FicheSteps() {    
    When("user want to create a {}", (EnumExtension extension) -> {
        TestUtils.navigateToContenu(extension);
        service.getScreenBO().createContent();
    });
}

The stacktrace :

cucumber.runtime.CucumberException: Failed to instantiate class fr.kosmos.selenium.cucumber.stepdefs.fiche.ActualiteSteps

    at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:47)
    at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:33)
    at cucumber.runtime.java.JavaBackend.buildWorld(JavaBackend.java:122)
    at cucumber.runner.Runner.buildBackendWorlds(Runner.java:120)
    at cucumber.runner.Runner.runPickle(Runner.java:38)
    at cucumber.api.testng.TestNGCucumberRunner.runScenario(TestNGCucumberRunner.java:68)
    at fr.kosmos.selenium.cucumber.KSupCucumberTestCase.runCucumberTest(KSupCucumberTestCase.java:61)
    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:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:41)
    ... 30 more
Caused by: cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions in FicheSteps.java:27 and FicheSteps.java:27
    at cucumber.runner.Glue.addStepDefinition(Glue.java:30)
    at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:159)
    at cucumber.api.java8.Fr.Quand(Fr.java:3985)
    at fr.kosmos.selenium.cucumber.api.FicheSteps.<init>(FicheSteps.java:27)
    at fr.kosmos.selenium.cucumber.stepdefs.fiche.ActualiteSteps.<init>(ActualiteSteps.java:19)
    ... 35 more

The strange thing i've noticed is that the duplicate exception appear on the same step definition :

Caused by: cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions in FicheSteps.java:27 and FicheSteps.java:27

Something important is that cucumber worked fine with all these steps until today. Any idea ?

samixchoumi commented 5 years ago

I think i've found the problem after some search, FicheSteps is extended by at least 2 classes, when i extend it on only 1 class, the problem disappear. So how it's possible to extends one step class ?

mlvandijk commented 5 years ago

@samixchoumi Could you please use the support forums for questions? It would be easier to help you there :)

mpkorstanje commented 5 years ago

Extending classes with step definitions always results in duplicates step definitions. That is why it is forbidden.

https://github.com/cucumber/cucumber-jvm/issues/301

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.