Skip to content

Commit

Permalink
Merge pull request #7335 from murdos/clean-cucumber-setup
Browse files Browse the repository at this point in the history
Run Cucumber tests with JUnit5
  • Loading branch information
pascalgrimaud authored Sep 14, 2023
2 parents 9b8f1b7 + 4fb1bef commit cac768a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 67 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -235,8 +235,8 @@
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ private CucumbersModules() {}
public static JHipsterModuleBuilder cucumberModuleBuilder(JHipsterModuleProperties properties) {
return moduleBuilder(properties)
.javaDependencies()
.addDependency(cucumberJunitDependency())
.addDependency(cucumberJunitPlatformEngineDependency())
.addDependency(cucumberJavaDependency())
.addDependency(cucumberSpringDependency())
.addDependency(junitVintageDependency())
.addDependency(junitPlatformSuiteDependency())
.and();
}

private static JavaDependency cucumberJunitDependency() {
private static JavaDependency cucumberJunitPlatformEngineDependency() {
return javaDependency()
.groupId(CUCUMBER_GROUP_ID)
.artifactId("cucumber-junit")
.artifactId("cucumber-junit-platform-engine")
.versionSlug(CUCUMBER_VERSION)
.scope(JavaDependencyScope.TEST)
.build();
Expand All @@ -50,7 +50,7 @@ private static JavaDependency cucumberSpringDependency() {
.build();
}

private static JavaDependency junitVintageDependency() {
return javaDependency().groupId("org.junit.vintage").artifactId("junit-vintage-engine").scope(JavaDependencyScope.TEST).build();
private static JavaDependency junitPlatformSuiteDependency() {
return javaDependency().groupId("org.junit.platform").artifactId("junit-platform-suite").scope(JavaDependencyScope.TEST).build();
}
}
6 changes: 3 additions & 3 deletions src/main/resources/generator/dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -191,8 +191,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package {{packageName}}.cucumber;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import static io.cucumber.junit.platform.engine.Constants.*;

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.Suite;
import {{packageName}}.ComponentTest;

@Suite(failIfNoTests = false)
@ComponentTest
@RunWith(Cucumber.class)
@CucumberOptions(
glue = "{{packageName}}",
plugin = {
"pretty", "json:target/cucumber/cucumber.json", "html:target/cucumber/cucumber.htm", "junit:target/cucumber/TEST-cucumber.xml",
},
features = "src/test/features"
@IncludeEngines("cucumber")
@SuppressWarnings("java:S2187")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "{{packageName}}")
@ConfigurationParameter(
key = PLUGIN_PROPERTY_NAME,
value = "pretty, json:target/cucumber/cucumber.json, html:target/cucumber/cucumber.htm, junit:target/cucumber/TEST-cucumber.xml"
)
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "src/test/features")
public class CucumberTest {}
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,12 @@ Behind the scene, your last service will be recalled until the assertions are OK
You may need to mock beans for your component tests but you won't be able to do it in a "classic" way (using `@MockBean`) since the application context will be already loaded. A way to achieve that is to overload beans to have mocks:

```java
@RunWith(Cucumber.class)
@ActiveProfiles("test")
@CucumberContextConfiguration
@CucumberOptions(
glue = "{{packageName}}",
plugin = {
"pretty",
"json:target/cucumber/cucumber.json",
"html:target/cucumber/cucumber.htm",
"junit:target/cucumber/cucumber.xml"
},
features = "src/test/features"
)
@SpringBootTest(
classes = {
{{ baseName }}App.class,
CucumberConfiguration.class,
CucumberMocksConfiguration.class
},
webEnvironment = WebEnvironment.RANDOM_PORT
)
public class CucumberTest {
@SpringBootTest(classes = { JhipsterSampleApplicationApp.class, CucumberMocksConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
public class CucumberConfiguration {
// other code omitted
@TestConfiguration
public static class CucumberMocksConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package {{packageName}}.cucumber;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import static io.cucumber.junit.platform.engine.Constants.*;

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.Suite;
import {{packageName}}.ComponentTest;

@Suite(failIfNoTests = false)
@ComponentTest
@RunWith(Cucumber.class)
@CucumberOptions(
glue = { "{{packageName}}", "tech.jhipster.lite.module.infrastructure.primary" },
plugin = {
"pretty", "json:target/cucumber/cucumber.json", "html:target/cucumber/cucumber.htm", "junit:target/cucumber/TEST-cucumber.xml",
},
features = "src/test/features"
@IncludeEngines("cucumber")
@SuppressWarnings("java:S2187")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "{{packageName}}, tech.jhipster.lite.module.infrastructure.primary")
@ConfigurationParameter(
key = PLUGIN_PROPERTY_NAME,
value = "pretty, json:target/cucumber/cucumber.json, html:target/cucumber/cucumber.htm, junit:target/cucumber/TEST-cucumber.xml"
)
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "src/test/features")
public class CucumberTest {}
23 changes: 13 additions & 10 deletions src/test/java/tech/jhipster/lite/cucumber/CucumberTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package tech.jhipster.lite.cucumber;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import static io.cucumber.junit.platform.engine.Constants.*;

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.Suite;
import tech.jhipster.lite.ComponentTest;

@Suite
@ComponentTest
@RunWith(Cucumber.class)
@CucumberOptions(
glue = "tech.jhipster.lite",
plugin = {
"pretty", "json:target/cucumber/cucumber.json", "html:target/cucumber/cucumber.htm", "junit:target/cucumber/TEST-cucumber.xml",
},
features = "src/test/features"
@IncludeEngines("cucumber")
@SuppressWarnings("java:S2187")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "tech.jhipster.lite")
@ConfigurationParameter(
key = PLUGIN_PROPERTY_NAME,
value = "pretty, json:target/cucumber/cucumber.json, html:target/cucumber/cucumber.htm, junit:target/cucumber/TEST-cucumber.xml"
)
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "src/test/features")
public class CucumberTest {}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ void shouldBuildInitialModule() {
.hasFiles("documentation/cucumber.md")
.hasFiles("src/test/features/.gitkeep")
.hasFile("pom.xml")
.containing("<artifactId>cucumber-junit</artifactId>")
.containing("<artifactId>cucumber-junit-platform-engine</artifactId>")
.containing("<artifactId>cucumber-java</artifactId>")
.containing("<artifactId>cucumber-spring</artifactId>")
.containing("<artifactId>junit-vintage-engine</artifactId>")
.containing("<artifactId>junit-platform-suite</artifactId>")
.containing("<artifactId>awaitility</artifactId>")
.containing("<version>${cucumber.version}</version>")
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ void shouldBuildModule() {
//@formatter:off
assertThatModuleWithFiles(module, pomFile(), mainAppFile())
.hasFile("pom.xml")
.containing("<artifactId>cucumber-junit</artifactId>")
.containing("<artifactId>cucumber-junit-platform-engine</artifactId>")
.containing("<artifactId>cucumber-java</artifactId>")
.containing("<artifactId>cucumber-spring</artifactId>")
.containing("<artifactId>junit-vintage-engine</artifactId>")
.containing("<artifactId>junit-platform-suite</artifactId>")
.containing(
"""
<dependency>
Expand Down Expand Up @@ -76,7 +76,7 @@ void shouldBuildModule() {
"src/test/java/tech/jhipster/test/security/infrastructure/primary/CorsFilterConfigurationIT.java"
)
.hasFile("src/test/java/com/jhipster/test/cucumber/CucumberTest.java")
.containing("glue = { \"com.jhipster.test\", \"tech.jhipster.lite.module.infrastructure.primary\" },")
.containing("key = GLUE_PROPERTY_NAME, value = \"com.jhipster.test, tech.jhipster.lite.module.infrastructure.primary\"")
.and()
.hasFile("src/test/java/com/jhipster/test/cucumber/CucumberConfiguration.java")
.containing("import com.jhipster.test.MyappApp;")
Expand Down

0 comments on commit cac768a

Please sign in to comment.