0

I have the following configuration for my tests:

Under the resources -> features folder file I have the users and moment feature files:

Users.feature

Feature: Validate the user related endpoints

  Background: Set up user
    Given I add a user to the DB

  Scenario: Validate fields for get users endpoint
    Given I access the get users endpoint
    And I get a 200 successful response
    And The response has all the expected fields for the get users endpoint

In Moments.feature

Feature: Moment Posting and Commenting

  Background: Set up user
    Given I add a user to the DB

  Scenario: User creates moment and that gets added to his existing list of moments
    And the user creates a moment with some basic and simple content
    Then the moment should be created successfully
    And the user should be able to see the moment in his list of moments

Then this entry point:

@Suite
@SelectClasspathResource("features")
@ConfigurationParameter(
        key = Constants.GLUE_PROPERTY_NAME,
        value = "com.example.hellotalk.steps")
public class HelloTalkTest {
}

And here is my configuration, which is placed under the steps -> config folder:

@Profile("dev")
@CucumberContextConfiguration
@SpringBootTest(classes = TestConfig.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CucumberSpringConfiguration extends BasePostgresConfig {

    @LocalServerPort
    private int port;

    @PostConstruct
    public void setup() {
        System.setProperty("port", String.valueOf(port));
    }
}

@ComponentScan(basePackages = {"com.example.hellotalk"})
@EnableAutoConfiguration
public class TestConfig {

}

My steps are defined under the steps folder and their specific subfolders, where UserStep is under steps -> user and MomentStep is under step -> moment

IntelliJ shows me the steps inside the step classes matching the ones declared within each feature file. However, when I run the tests that call steps declared inside MomentStep.java they fail with a message saying they haven’t been implemented, despite as I mentioned before IntelliJ being able to navigate to them and show me their usages.

I think it may perhaps be something to do with ScenarioScope and context, but both moment and user have a context class declared inside their packages (even though the one for Moment has only been added to debug the issue, as it’s empty since there is not a need for it to be sharing variables with other steps so far).

@Data
@Component
@ScenarioScope
public class MomentContext {

}

@Data
@Component
@ScenarioScope
public class UserContext {

    private List<UserEntity> userListDB;
}


@RequiredArgsConstructor
@Data
public class UserStep {

    private final ApiStep apiStep;
    private final UserContext uc;

    @Then("I validate the response for the get users endpoint against the database")
    public void iValidateResponseForGetUsersEndpointAgainstTheDatabase() {
        List<User> userListApi = Arrays.asList(apiStep.getResponse().as(User[].class));
        List<UserEntity> userListDB = uc.getUserListDB();

        for (UserEntity userDB : userListDB) {

            Optional<User> matchingUser = findApiUserMatchingDB(userListApi, userDB);

            if (matchingUser.isPresent()) {
                User userApi = matchingUser.get();

                assertThat(userApi)
                        .usingRecursiveComparison()
                        .ignoringFields("hometown", "hobbyAndInterests", "followerOf", "followedBy")
                        .isEqualTo(userDB);
            }
        }
    }

    private Optional<User> findApiUserMatchingDB(List<User> userListApi, UserEntity userDB) {
        return userListApi.stream().filter(userApi -> userApi.getId().equals(userDB.getId())).findFirst();
    }
}


@RequiredArgsConstructor
@Data
public class MomentStep {

    private final MomentContext mc;

    private final ApiStep apiStep;
    private final RestClient restClient;

    @And("the user creates a moment with some basic and simple content")
    public void theUserCreatesAMomentWithSomeBasicAndSimpleContent() {
        Moment moment = Moment.builder().text("A great day testing").build();

        RequestSpecification rq = restClient.getRequestSpecification();
        apiStep.setResponse(rq.body(moment).post("/api/v1/ht/moments/"));
    }

    @Then("the moment should be created successfully")
    public void iGetResponse() {
        apiStep.getResponse().then().statusCode(201);
    }

    @And("the user should be able to see the moment in their list of moments")
    public void userSeeMomentInMomentList() {
        Moment moment = apiStep.getResponse().as(Moment.class);

        RequestSpecification rq = restClient.getRequestSpecification();
        apiStep.setResponse(rq.get("/api/v1/ht/moments/"));

        List<Moment> momentList = Arrays.asList(apiStep.getResponse().as(Moment[].class));

        boolean isMomentFound = momentList.stream().anyMatch(m -> m.getId().equals(moment.getId()));
        Assertions.assertTrue(isMomentFound);
    }
}

Thank you.

https://github.com/francislainy/HelloTalk/tree/master/src/test

PS: I've also tried things such as invalidating caches and restarting IntelliJ, but that hasn't solved the issue.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81

1 Answers1

0

I have just found out IntelliJ was the one causing the issue. Running the tests from maven worked no problem, or even running the suite from IntelliJ also works fine, but for some reason running the individual moment feature file by right clicking on it errors with the steps not implemented message.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81