I'm using cucumber-spring to create my integration tests and use DI provided by Spring. The way it's done: there is a separate project that defines all the necessary cucumber dependencies:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
The project also has a base test class annotated with SpringBootTest (that allows to load application context):
@RunWith(SpringRunner.class)
@SpringBootTest(
classes = {SomeBeans.class}
)
public class BaseTestStateSteps {
I'm adding this project as a dependency in the pom of my project:
<dependency>
<groupId>com.externalproject</groupId>
<artifactId>spring-test-cucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Then I have my runner class:
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "junit:target/cucumber/cucumber.xml", "json:target/cucumber/cucumber.json",
"html:target/cucumber/html/"},
tags = {"@Smoke"},
features = {"src/test/resources/features"},
glue = {"com.externalproject.steps","com.mycompany.steps"}
)
public class RunTestIT {
}
And I also have my Step class:
public class MyTestSteps {
@Autowired
ClassToAutowire classToAutowire;
@Given("^I can do something$")
public void generateRandomPhoneNumber() {
classToAutowirer.doSomething();
}
So the issue I'm running: - if I don't have my RunTestIT annotated with SpringBootTest or with ContextConfiguration - ClassToAutowire is null (it's not autowired); - if I addone of those annotations I'm running into error:
cucumber.runtime.CucumberException: Glue class class com.externalproject.steps.BaseTestStateSteps and class com.mycompany.steps.MyTestSteps both attempt to configure the spring context. Please ensure only one glue class configures the spring context
I need the functionality of Steps from external project (so I don't have to copy/paste from there and get the latest feature updates), but I also need dependencies from my spring context to be autowired.
I read the article https://thepracticaldeveloper.com/2018/03/31/cucumber-tests-spring-boot-dependency-injection/ But it only deals with multiple step definitions inside the same project.
Do you have any ideas how I can solve the issue?