0

My test context (using the spring annotation @Component and wired to my steps with @Autowired) is dirty after a scenario, and I want it to auto-reset so the scenario coming next has a clean context before it starts.

I can manually do it with the cucumber annotation @Before/@After, but then it will show up in the report, and I don't want that because it has no business value.


Runner class

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/features",
        glue = "com.company.steps",
        tags = "@smoke",
        plugin = {"pretty", "json:target/cucumber-report/cucumber.json", "html:target/cucumber-report/html"})
public class RunCucumber {
}

cucumber.xml (spring configuration file)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.funky.company"/>

</beans>

Cucumber: 4.8.0

Spring: 5.2.0.RELEASE

Junit: 4.12

Flyout91
  • 782
  • 10
  • 31
  • 2
    Try with the DirtiesContext annotation on stepdefinition class https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html – Grasshopper Nov 07 '19 at 12:16
  • You are refering to something like https://stackoverflow.com/questions/37863502/dirtiescontext-tears-context-down-after-every-cucumber-test-scenario-not-class. I can not do that because I use @RunWith(Cucumber.class) and every scenario use steps scattered across various step definition class (grouped per domain, as recommended by the cucumber documentation) – Flyout91 Nov 07 '19 at 13:22
  • 2
    If you are using `cucumber-spring` you should have one step definition class annotated with some form of context configuration. That class should also have the `@DirtiesContext` annotation. – M.P. Korstanje Nov 07 '19 at 20:47
  • I am using cucumber-spring. I edited my post with the only class containing configuration, which is only JUnit and Cucumber configuration. For the spring context configuration I simply use cucumber.xml which species the package that spring should scan for annotations. – Flyout91 Nov 08 '19 at 08:36
  • 1
    To use `@DirtiesContext` you'll have to use annotation based configuration: https://github.com/cucumber/cucumber-jvm/tree/master/spring#annotation-based-configuration You can also add beans to the `cucumber-glue` context. These beans will be recycled after each scenario. – M.P. Korstanje Nov 09 '19 at 11:37

0 Answers0