Hello fellow engineers!
I have run into a problem when trying to create a FAT jar to execute the Cucumber tests. Initially, I have followed the guide to set up the tests from Baeldung. The tests are working fine when executed during the Maven test phase. It is also working as expected when running the mvn exec:java command with the parameters.
However, when I have a FAT jar created and I try to execute the tests I am faced with the error
java.util.concurrent.ExecutionException: io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.
For example:
@CucumberContextConfiguration
@SpringBootTest(classes = TestConfig.class)
public class CucumberSpringConfiguration { }
Or:
@CucumberContextConfiguration
@ContextConfiguration( ... )
Here is the explanation of my project, which is basically almost exactly as the test project at Baeldung.
RunCucumberTest.java
package com.ing.testsuite;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import io.cucumber.*;
@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources", glue="com.ing.testsuite", plugin = {"pretty","html:target/cucumber.html"})
class RunCucumberTest {
public static void main(String[] args) throws Throwable {
String[] arguments = {"classpath:dummy.feature", "classpath:com.ing.testsuite"};
io.cucumber.core.cli.Main.main(arguments);
}
}
CucumberSpringConfiguration.java
package com.ing.testsuite;
import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
@CucumberContextConfiguration
@SpringBootTest(classes = RunCucumberTest.class)
public class CucumberSpringConfiguration{
}
StepDefinitions.java
package com.ing.testsuite;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
public class StepDefinitions extends CucumberSpringConfiguration{
private Integer noOfCucumbers;
private Integer noOfCucumberEaten;
private Integer noOfRemainingCucumber;
@Given("There are {int} cucumbers")
public void cucumberTest_nrOne(Integer noOfCucumber) throws Throwable{
noOfCucumbers = noOfCucumber;
}
@When("I eat {int} cucumbers")
public void cucumberTest_nrTwo(Integer noOfCucumberEaten) throws Throwable{
noOfCucumbers = noOfCucumbers - noOfCucumberEaten;
}
@Then("I should have {int} cucumbers")
public void cucumberTest_nrThree(Integer noOfRemainingCucumber) throws Throwable{
assertEquals(noOfCucumbers,noOfRemainingCucumber);
}
}
dummy.feature
Feature: This is dummy test scenario
Scenario Outline: Eating
Given There are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
| 30 | 5 | 25 |
assembly.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**/*.*</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.feature</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
Some of the resources I have already tried to follow
- Running Cucumber tests directly from executable jar
- SpringRunner unable to detect configuration
- Spring context from within a jar file
- Is additional context configuration required when upgrading cucumber-jvm from version 4 to version 6?
- Initialization Error while executing TestRunner class
I would really appreciate if someone could help me out.