Search code examples
spring-bootcucumbercucumber-jvmcucumber-javacucumber-junit

Running same feature file with two different Step definitions in a Springboot application


I am testing a Springboot application using cucumber (v7.2.3). I have a feature file to test application using @SpringBootTest and @Suite (from junit-platform) I am using @ConfigurationParameter to refer the package where I have my step definition class.

I also have @DataTableType annotations to convert feature data into java objects, which are in the same package as my step-definitions.

Now I want to have another set of step definitions (in a separate file) which will connect to the application running in dev environment (over REST). I want to re-use my feature files as testing exactly same feature but on a running application.

I tried to create a new step definition java class in a different package and placed existing step definition java class in another package, but feature data to java object creation failed. Probably cucumber is expecting to have class which has @DataTableType annotations in the same package as step definition class.

Any idea how can I achieve 2 different step definitions using same feature file.

Note: I want to run local step definitions to run as part of maven build and remote step definitions as post deployment task. Please also guide how can I achieve that (probably using tags).

Versions used junit-platform - 1.8.2 junit-jupiter - 5.8.2 springboot - 2.7.4


Solution

  • Suppose you organize your code into three packages com.example.local, com.example.remote, com.example.shared. Then you would put the step definitions for the local tests in .local, those for remote in .remote and the data table types and other shared code in the .shared package.

    Then you can create multiple suites with different configurations for the glue.

    @Suite
    @IncludeEngines("cucumber")
    @SelectClasspathResource("com/example")
    @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.local, com.example.shared")
    public class RunLocalCucumberTest {
    }
    
    @Suite
    @IncludeEngines("cucumber")
    @SelectClasspathResource("com/example")
    @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.remote, com.example.shared")
    public class RunRemoteCucumberTest {
    }