Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot/Cucumber Integration-How to use @Autowired(or other SB tags) inside step definition class?

I'm trying to create a Springboot, cucumber and Junit4 automation framework. The versions I used as below:

  • Springboot: 2.1.3
  • cucumber: io.cucumber 4.2.3
  • Junit4: 4.12
  • Operating System: Win7 Pro.

I created a prop class which trying to get properties from property file (.yml)

Prop Class:

@Data
@Component
public class PropsConfig {
    @Value("${spring.skyewss}")
    public String url;
}

Step defs:

public class SkyeWssLoginStepDef implements En {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    private WebDriver driver;
    private SkyeWssLoginPage loginPage;
    private SkyeWssUtil skyeWssUtil;
    @Autowired
    private PropsConfig propsConfig;

    public SkyeWssLoginStepDef() {


        Given("^I open Skye WSS web page$", () -> {
            driver = CukeHook.driver;
            loginPage = new SkyeWssLoginPage(driver);
            driver.get(propsConfig.getUrl());
            skyeWssUtil = new SkyeWssUtil();
            LOGGER.info("Current page is " + driver.getTitle());
        });
       }
       ......
     }

Cucumber runner class:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"src/test/resources/features"},
        plugin = {"pretty", "html:target/cucumber-html-report"},
        tags = {"@SkyeWss"}
)
@SpringBootTest
public class WssRegApplicationTests {

}

I've tried to give tags on stepdef classes, but no luck. When I give stepdef classes tags like @Component or @SrpingBootTest, I will get error.

cucumber.runtime.CucumberException: Glue class class com.flexicards.wss_reg.skye.step.SkyeWssLoginStepDef and class com.flexicards.wss_reg.skye.step.SkyeWssDashboardValStepDef both attempt to configure the spring context. Please ensure only one glue class configures the spring context


cucumber.runtime.CucumberException: Glue class com.flexicards.wss_reg.skye.step.SkyeWssDashboardValStepDef was annotated with @Component; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Component annotation

I'm new to Spring and Springboot, I'm pretty sure I did not configure correctly in somewhere. Most example for springboot and cucumber out there are outdated. I've tried them already. Like create an abstract classes which extended by all the stepdefs classes. This will give me the same error as @SpringBootTest one.

Could anyone help me on this? Any inputs are welcomed. Thank you very much.

like image 949
Shi Tim Avatar asked Feb 25 '19 19:02

Shi Tim


1 Answers

Looks like you've done almost everything right. The only thing out of place is the the location of your context configuration. It has to be in a file with a step or hook definition. Otherwise Cucumber won't detect it. This should do the trick:

@SpringBootTest
@AutoConfigureMockMvc
public class CucumberContextConfiguration  {

    @Before
    public void setup_cucumber_spring_context(){
        // Dummy method so cucumber will recognize this class as glue
        // and use its context configuration.
    }
} 

You can find a working example of cucumber-spring in the cucumber github repository.

Perhaps also worth to keep in mind that Cucumber implements step definitions as spring beans rather then post processed unit test classes as you might expect. This means that @MockBean, @SpyBean and friends won't work.

edit:

With Cucumber v6.0.0 you can omit the dummy method and instead use the @CucumberContextConfiguration annotation.

@SpringBootTest
@CucumberContextConfiguration
public class CucumberContextConfiguration  {

} 
like image 138
M.P. Korstanje Avatar answered Nov 11 '22 18:11

M.P. Korstanje