Forum Discussion

mvipat's avatar
mvipat
New Contributor
4 years ago

Before and After

I am using Cucumber version 6.10.2 and wanted to invoke Before and After each scenario is finished

 

build. gradle

-------------------------

testImplementation "io.cucumber:cucumber-core:${cucumberVersion}"
testImplementation "io.cucumber:cucumber-java:${cucumberVersion}"
testImplementation "io.cucumber:cucumber-java8:${cucumberVersion}"
testImplementation "io.cucumber:cucumber-testng:${cucumberVersion}"
testImplementation "io.cucumber:cucumber-spring:${cucumberVersion}"

BehaviorITRunner class

import com.xxx.customer.behavior.endpoint.EndpointsController;
import io.cucumber.java.*;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;

@Slf4j
@CucumberOptions(plugin = { "json:test-output/cucumber-report/b2b-myaccount.json", "html:test-output/cucumber-report/b2b-myaccount.html" },
features = "src/test/resources")
public class BehaviorRunnerIT extends AbstractTestNGCucumberTests{

private EndpointsController endpointsController = EndpointsController.getInstance();

private BehaviorSuiteSetter suiteSetter;

@BeforeSuite
public void beforeSuite() {
suiteSetter = BehaviorSuiteSetter.getInstance();
this.suiteSetter.beforeSuite();
}

@AfterSuite
public void afterSuite() {
this.suiteSetter.afterSuite();
}

@Before
public void beforeScenario() {
//public void beforeScenario(Scenario scenario) {
//log.error("Executing Scenario "+scenario.getName());
}

@After
public void afterScenario() {
this.endpointsController.refresh();
}

@Override
@DataProvider
public Object[][] scenarios() {
return super.scenarios();
}
}

 I am getting below exception

io.cucumber.core.exception.CucumberException: Could not invoke hook defined at 'com.xxx.customer.behavior.BehaviorRunnerIT.beforeScenario()'.
It appears there was a problem with the hook definition.

at io.cucumber.core.runner.HookDefinitionMatch.couldNotInvokeHook(HookDefinitionMatch.java:30)
at io.cucumber.core.runner.HookDefinitionMatch.runStep(HookDefinitionMatch.java:23)
at io.cucumber.core.runner.ExecutionMode$1.execute(ExecutionMode.java:10)
at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:92)
at io.cucumber.core.runner.TestStep.run(TestStep.java:64)
at io.cucumber.core.runner.TestCase.run(TestCase.java:98)
at io.cucumber.core.runner.Runner.runPickle(Runner.java:73)
at io.cucumber.testng.TestNGCucumberRunner.lambda$runScenario$0(TestNGCucumberRunner.java:117)
at io.cucumber.core.runtime.CucumberExecutionContext.runTestCase(CucumberExecutionContext.java:117)
at io.cucumber.testng.TestNGCucumberRunner.runScenario(TestNGCucumberRunner.java:114)
at io.cucumber.testng.AbstractTestNGCucumberTests.runScenario(AbstractTestNGCucumberTests.java:31)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
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 com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: io.cucumber.core.backend.CucumberBackendException: No qualifying bean of type 'com.xxx.customer.behavior.BehaviorRunnerIT' available: expected single matching bean but found 2: behaviorRunnerIT,com.xxx.customer.behavior.BehaviorRunnerIT
at io.cucumber.spring.SpringFactory.getInstance(SpringFactory.java:165)
at io.cucumber.java.AbstractGlueDefinition.invokeMethod(AbstractGlueDefinition.java:47)
at io.cucumber.java.JavaHookDefinition.execute(JavaHookDefinition.java:59)
at io.cucumber.core.runner.CoreHookDefinition.execute(CoreHookDefinition.java:46)
at io.cucumber.core.runner.HookDefinitionMatch.runStep(HookDefinitionMatch.java:21)
... 32 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.xxx.customer.behavior.BehaviorRunnerIT' available: expected single matching bean but found 2: behaviorRunnerIT,com.xxx.customer.behavior.BehaviorRunnerIT
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:422)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:352)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
at io.cucumber.spring.TestContextAdaptor.getInstance(TestContextAdaptor.java:103)
at io.cucumber.spring.SpringFactory.getInstance(SpringFactory.java:163)
... 36 more

  • In your stacktrace, I've spotted the following:

    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.xxx.customer.behavior.BehaviorRunnerIT' available: expected single matching bean but found 2: behaviorRunnerIT,com.xxx.customer.behavior.BehaviorRunnerIT

     Does that help?

    I am not familiar with Java so I am not sure. Could you make sure your test suite, and everything, is ok? Do you have any kind of duplication?

    • mvipat's avatar
      mvipat
      New Contributor

      Yes I noted that too but once I remove the Hook method everything works fine.

       

      @Before
      public void beforeScenario(Scenario scenario){

      }
      • Would you have the possibility to try to write a minimal reproduction example?

         

        That would help trying to figure out what is going on here