Is additional context configuration required when upgrading cucumber-jvm from version 4 to version 6?
Asked Answered
T

3

8

I am using cucumber-jvm to perform some functional tests in Kotlin.

I have the standard empty runner class:

@RunWith(Cucumber::class)
@CucumberOptions(features=[foo],
    glue=[bar],
    plugin=[baz],
    strict=true,
    monochrome=true)
class Whatever

The actual steps are defined in another class with the @ContextConfiguration springframework annotation. This class also uses other spring features like @Autowire or @Qualifier

@ContextConfiguration(locations=["x/y/z/config.xml"])
class MyClass {
    ...
    @Before
    ...

    @Given("some feature file stuff")
    ...

    // etc
}

This all work fine in cucumber version 4.2.0, however upgrading to version 6.3.0 breaks things. After updating the imports to match the new cucumber project layout the tests now fail with this error:

io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.

It provides examples of what it means...

For example:

    @CucumberContextConfiguration
    @SpringBootTest(classes = TestConfig.class)
    public class CucumberSpringConfiguration {}

Or:

    @CucumberContextConfiguration
    @ContextConfiguration( ... )
    public class CucumberSpringConfiguration {}

It looks like it's telling me I can just add @CucumberContextConfiguration to MyClass.

But why?

I get the point of @CucumberContextConfiguration, it's explained well here but why do I need it now with version 6 when version 4 got on fine without it? I can't see any feature that was deprecated and replaced by this.

Any help would be appreciated :)

Trout answered 24/8, 2020 at 14:20 Comment(0)
T
16

Since the error matches exactly with the error I was getting in running Cucumber tests with Spring Boot, so I am sharing my fix.

One of the probable reason is: Cucumber can't find the CucumberSpringConfiguration class in the glue path.

Solution 1:

Move the CucumberSpringConfiguration class inside the glue path (which in my case was inside the steps package).

Solution 2:

Add the CucumberSpringConfiguration package path in the glue path.


The below screenshot depicts my project structure.

Project structure

As you can see that my CucumberSpringConfig class was under configurations package so it was throwing me the error when I tried to run feature file from command prompt (mvn clean test):

"Please annotate a glue class with some context configuration."

So I applied solution 2, i.e added the configurations package in the glue path in my runner class annotation.

Fix 2

And this is the screenshot of the contents of CucumberSpringConfiguration class:

CucumberSpringConfiguration

Just an extra info:

To run tests from command prompt we need to include the below plugin in pom.xml plugin to run test from command prompt

Theurer answered 7/8, 2021 at 16:10 Comment(1)
For some weird reason, Solution 2 did not work for me but Solution 1 worked by moving the file itself to steps package. Thanks :-)Arium
C
5

https://github.com/cucumber/cucumber-jvm/pull/1959 removed the context configuration auto-discovery. The author concluded that it hid user errors and removing it would provide more clarity and reduce complexity. It also listed the scenarios where the context configuration auto-discovery used to apply.

Note that it was introduced after https://github.com/cucumber/cucumber-jvm/pull/1911, which you had mentioned.

Charade answered 23/9, 2020 at 6:56 Comment(0)
C
0

Had the same error but while running Cucumber tests from Jar with Gradle.

The solution was to add a rule to the jar task to merge all the files with the name "META-INF/services/io.cucumber.core.backend.BackendProviderService" (there could be multiple of them in different Cucumber libs - cucumber-java, cucumber-spring).

For Gradle it is:

 shadowJar {
     ....
     transform(AppendingTransformer) {
        resource = 'META-INF/services/io.cucumber.core.backend.BackendProviderService'
     }
 }

For Maven something like this:

<transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
        <resource>META-INF/services/io.cucumber.core.backend.BackendProviderService</resource>
    </transformer>
</transformers>

A bit more explanation could be found in this answer

Cerargyrite answered 28/4, 2022 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.