Search code examples
automated-testsgherkincucumber-java

Can you save a result (Given) to a variable in a Gherkin feature file, and then compare the variable with another result (Then)? (Cucumber for Java)


I am new to Cucumber for Java and trying to automate testing of a SpringBoot server backed by a MS SQL Server.

I have an endpoint "Get All Employees".

Writing the traditional feature file, I will have to list all the Employees in the @Then clause. This is not possible with thousands of employees.

So I just want to get a row count of the Employee table in the database, and then compare with the number of objects returned from the "Get All Employees" endpoint.

Compare

SELECT count(*) from EMPLOYEE

with size of the list returned from

List<Employee> getAllEmployees()

But how does one save the rowcount in a variable in the feature file and then pass it into the stepdefs Java method?

I have not found any way that Gherkin allows this.


Solution

  • After writing a few scenario and feature files, I understood this about Cucumber and fixed the issue.

    Gherkin/Cucumber is not a programming language. It is just a specification language. When keywords like Given, Then are reached by the interpreter, the matching methods in Java code are called. So they are just triggers.

    These methods are part of a Java glue class. Data is not passed out of the Java class and into the gherkin feature file. The class is instantiated at the beginning and retained until the end. Because of this, it can store state.

    So from my example in the question above, the Then response from a Spring endpoint call will be stored in a member variable in the glue class. The next Then invocation to verify the result will call the corresponding glue method which will access the data in the member variable to perform the comparison.

    So Gherkin cannot do this, but Java at a lower level in the glue class, can.