Issue
I am trying to write integration tests using cucumber + Spring boot + Junit5
I have these dependencies and my gradle file is this
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation("io.rest-assured:rest-assured:4.4.0")
testImplementation("io.cucumber:cucumber-java:6.10.4")
testImplementation("io.cucumber:cucumber-spring:6.10.4")
testImplementation("io.cucumber:cucumber-junit-platform-engine:6.10.4")
testRuntimeOnly("org.junit.platform:junit-platform-console")
// https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-api
testImplementation group: 'org.junit.platform', name: 'junit-platform-suite-api', version: '1.9.0'
}
tasks.named('test') {
useJUnitPlatform()
}
And I have two classes
Runner class
package com.abc.catalog;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/abc/catalog")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.abc.catalog")
public class CucumberRunnerTests {
}
Test Class
package com.abc.catalog;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsIterableContaining.hasItem;
import static org.hamcrest.core.StringContains.containsString;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.spring.CucumberContextConfiguration;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringbootCucumberTestDefinitions {
private final static String BASE_URI = "http://localhost";
@LocalServerPort
private int port;
private ValidatableResponse validatableResponse;
private void configureRestAssured() {
RestAssured.baseURI = BASE_URI;
RestAssured.port = port;
}
protected RequestSpecification requestSpecification() {
configureRestAssured();
return given();
}
@Given("I send a request to the URL {string} to get user details")
public void iSendARequest(String endpoint) throws Throwable {
validatableResponse = requestSpecification().contentType(ContentType.JSON)
.when().get(endpoint).then();
System.out.println("RESPONSE :"+validatableResponse.extract().asString());
}
@Then("the response will return status {int} and id {int} and names {string} and passport_no {string}")
public void extractResponse(int status, int id, String studentName,String passportNo) {
validatableResponse.assertThat().statusCode(equalTo(status))
.body("id",hasItem(id)).body(containsString(studentName))
.body(containsString(passportNo));
}
}
The features are present at src/test/resuorces/com/abc/catalog
When I ran the tests CucumberRunnerTests, its not detecting the tests
> No tests found for given includes: [com.abc.catalog.CucumberRunnerTests](--tests filter)
Can some one help me to fix this
Solution
I am able to made work with this
plugins {
id 'org.springframework.boot' version "${gradleSpringBootPluginVersion}"
}
dependencies {
implementation project(':catalog-common')
implementation project(':catalog-model')
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: "${openApiDocsVersion}"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation("io.rest-assured:rest-assured:4.4.0")
testImplementation("io.cucumber:cucumber-java:7.6.0")
testImplementation("io.cucumber:cucumber-spring:7.6.0")
testImplementation("io.cucumber:cucumber-junit-platform-engine:7.6.0")
testImplementation group: 'org.junit.platform', name: 'junit-platform-console', version: '1.9.0'
testImplementation group: 'org.junit.platform', name: 'junit-platform-suite-api', version: '1.9.0'
}
tasks.named('test') {
useJUnitPlatform()
}
Class
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameters({
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME,
value = "json:build/cucumber/cucumber.json"),
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.abc.catalog.bdd"),
@ConfigurationParameter(key = FILTER_TAGS_PROPERTY_NAME, value = "not @Disabled"),
@ConfigurationParameter(key = PLUGIN_PUBLISH_QUIET_PROPERTY_NAME, value = "true"),
@ConfigurationParameter(key = JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, value = "long")
})
public class CatalogBddFeatures {
}
@CucumberContextConfiguration
@SpringBootTest(classes = CatalogApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
@TestPropertySource("classpath:test-application.properties")
public class SpringGlue {
}
And placed features under src/test/resources/features
Answered By - Patan
Answer Checked By - Clifford M. (JavaFixing Volunteer)