question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple step definitions with spring context

See original GitHub issue

This check

    public boolean addClass(final Class<?> stepClass) {
        if (!stepClasses.contains(stepClass)) {
            checkNoComponentAnnotations(stepClass);
            if (dependsOnSpringContext(stepClass)) {
                if (stepClassWithSpringContext != null) {
                    throw new CucumberException(String.format("" +
                        "Glue class %1$s and %2$s both attempt to configure the spring context. Please ensure only one " +
                        "glue class configures the spring context", stepClass, stepClassWithSpringContext));
                }
                stepClassWithSpringContext = stepClass;
            }
            stepClasses.add(stepClass);
        }
        return true;
    }

in SpringFactory actually prevents usage of multiple step definitions which have to use spring context.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
Ben1980commented, Aug 30, 2018

I fixed it by doing the following…

A separate cucumber runner class:

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = { "pretty", "html:build/reports/cucumber" },
        glue = "com.whatever.feature.steps",
        features = "src/acceptance-test/resources/feature/"
)
public class AcceptanceTestsRunner {}

and a context loader class

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ContextLoader {
    @Before
    public void setUp() {
    }
}

Afterwards you can implement your step definitions and split them by their concerns. But make sure that ContextLoader is in the same package as the step definitions are, in my case com.whatever.feature.steps

5reactions
Ben1980commented, Jun 10, 2018

Because it’s good programming style to keep responsibilities separated. Not only sourcecode but also tests should follow SRP.

E.g. a feature file for Login, another feature file for registration, another feature file for doing something meaningful. So we already separate the features into separate feature files, The same should be done with the step definition. Instead of having one huge step definition which handles all different features, it should be separated by it’s responsibility.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to reuse cucumber steps from multiple projects ...
When using cucumber-spring Cucumber uses Spring's TestContextManager framework. This framework starts a test context using a single class.
Read more >
Sharing State Between Cucumber Step Definitions Using Java ...
In this article, let us see how we can simplify sharing state between various step definitions using a Singleton enum named TestContext. Let...
Read more >
How to share the Spring Boot context between steps...
Hi all, I am working on a cucumber integration test project for spring boot. Currently I have only one feature file, and I...
Read more >
Java Cucumber Tutorial 13 (Multiple Step Definition Classes)
Java Cucumber Tutorial 13 ( Multiple Step Definition Classes) | QAShahinJava Cucumber video tutorial on managing multiple Step Definition ...
Read more >
Sharing state between steps in Cucumber-JVM using Spring
A small example in two parts ... Lets look at a small example and see how it can be implemented with one step...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found