"mvn test" 0 tests run - spring + cucumber + junit5

Viewed 630

I have a test project where I'm trying to setup e2e api tests using rest-assured. Tests run fine if I run them from the feature files, however, when I try to run them with maven, 0 tests run. I believe there is something funky with my pom.xml but I can't figure it out...

My project structure looks like:this

My pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.0</version>
    </parent>

    <artifactId>qa-automation-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <groupId>com</groupId>
    <packaging>jar</packaging>

    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-bom</artifactId>
                <version>7.2.3</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-path</artifactId>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>xml-path</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <properties>
                            <configurationParameters>
                                cucumber.junit-platform.naming-strategy=long
                            </configurationParameters>
                        </properties>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

My Application.java

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@PropertySources({
        @PropertySource("classpath:application.properties")
})
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

My CucumberSpringConfiguration.class

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import com.Application;


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

My CucumberTest.java

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("src/test/resources/example")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example")

public class CucumberTest {

}

I'm not really familiar with Spring though so I'm pretty sure I'm not using it correctly in my ApiTestStepDef.java

package com.example;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Given;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import com.client.RestAssuredClient;
import com.model.User;
import com.utils.Helper;

import static io.restassured.RestAssured.given;

public class ApiTestStepDef {

    private Response response;
    private RequestSpecification request;
    private User user;
    private User responseBody;
    @Autowired
    private RestAssuredClient restAssuredClient;

    @Given("{string} endpoint")
    public void setBaseUsersURI(String url){
        request =
                given().log().all().
                spec(restAssuredClient.createReqSpec(url));
    }

    @When("user posts request with details {string} {string} {string}")
    public void sendRequest(String name, String gender, String status){

        user = new User(name, gender, Helper.createRandomEmail(), status);
        response =
         request.given().log().all().
                body(user).
                when().
                 post().
                 then().log().all().
                 extract().response();
    }

    @Then("response status code is {int} and response contains correct user details")
    public void checkResponseStatusCode(int statusCode){
        response.then().spec(restAssuredClient.createResSpec(statusCode));
        responseBody = response.getBody().as(User.class);
        Assertions.assertEquals(user.getGender(), responseBody.getGender());
        Assertions.assertEquals(user.getStatus(), responseBody.getStatus());
        Assertions.assertEquals(user.getEmail(), responseBody.getEmail());
        Assertions.assertEquals(user.getName(), responseBody.getName());
    }
}

And RestAssuredClient.java

package com.client;

import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static io.restassured.RestAssured.oauth2;

@Component
public class RestAssuredClient {
    @Value("${access.token}")
    private String accessToken;
    @Value("${base.uri}")
    private String baseUri;

    public ResponseSpecification createResSpec(int statusCode){
        return
                new ResponseSpecBuilder()
                        .expectStatusCode(statusCode)
                        .expectContentType(ContentType.JSON)
                        .build();
    }


    public RequestSpecification createReqSpec(String url){
       return new RequestSpecBuilder()
                    .setBaseUri(baseUri)
                    .setContentType(ContentType.JSON)
                    .setAuth(oauth2(accessToken))
               .setBasePath(url)
                    .build();
    }
}
1 Answers
@SelectClasspathResource("src/test/resources/example")

Typically src/test/resources is not part of the classpath.

After running mvn test have a look at target/test-classes to understand the structure of what is on the classpath.

Related