Flow - gorskip/testing-framework GitHub Wiki

Rest application and database

Rest request: http://localhost:8090/employee

Rest response:

[
   {
      "id": 1,
      "name": "John Smith",
      "salary": 5000
   },
   {
      "id": 2,
      "name": "Mambo Jumbo",
      "salary": 9000
   }
]

Database response:

query: select * from employee

ID|EMPLOYEE_NAME|SALARY
1 |"John Smith" |5000
2 |"Mambo Jumbo"|9000

Test preparation

Create Entity

@Data
@EqualsAndHashCode
public class Employee {

    private Long id;

    @Column(name = "EMPLOYEE_NAME")
    private String name;

    private Double salary;
}

Create Story definition

See also how to parametrize test data

{
  "name": "Employee module",
  "tests": [
    {
      "name": "Get all employees",
      "rest": {
        "request" : {
          "method": "GET",
          "url": "http://localhost:8090/employee",
          "headers": {
            "Accept": "application/json"
          }
        },
        "expected": {
          "status": 200
        }
      },
      "db": {
        "query": "select * from employee"
      }
    }
  ]
}

Create Story implementation

@Story("Employee module")
public class EmployeeTest extends StoryTest {

    @Test("Get all employees")
    public void getEmployeesTest(TestCase testCase)  {
       
    }
}

Create Test implementation

All you need to do is:

    @Test("Get all employees")
    public void getEmployeesTest(TestCase testCase) throws SQLException {
         verify(testCase, Employee.class);
    }

What does verify method do?

    @Test("Get all employees")
    public void getEmployeesTest(TestCase testCase) throws SQLException {
        //verify(testCase, Employee.class) { =>

        //prepare data for rest request
        Rest rest = testCase.getRest();
        Request request = rest.getRequest();

        //prepare expected data
        Expected expected = rest.getExpected();

        //execute request and get response
        Response<List<Employee>> restResponse = restClient().execute(request);

        //verify if giver response is as expected
        new VerifyIf().given(restResponse)
                .has(expected)
                .status()
                .and(expected)
                .body()
                .and()
                .headers();

        //prepare data for db request
        Db db = testCase.getDb();
        String sql = db.getQuery();

        //execute sql query and get results
        List<Employee> queryResult = dbClient().query(sql, Employee.class);

        //verify if rest response and databse response are the same
        //uses Arrays.deepEquals
        compare(restResponse, queryResult, Employee.class);
        //if Employee class has Validator validate entity fields
        validate(restResponse);
        validate(queryResult);
    }

Validation

Create Validator and add to Employee class

@Data
@EqualsAndHashCode

@Validator(name = EmployeeValidator.class)
public class Employee {
public class EmployeeValidator extends Validator<Employee> {

    @Override
    void validate(Employee entity) {

    }
}
    @Override
    void validate(Employee entity) {
        hasMinSize(entity.getName(), 15, "Employee name");
    }

All methods:

validation methods

Test result

Caused by: pl.pg.exception.AssertionException: [ {
  "object" : "Employee(id=1, name=John Smith, salary=5000.0)",
  "fieldValidationSummaries" : [ {
    "fieldName" : "Employee name",
    "validationType" : "MIN_SIZE",
    "actual" : 10,
    "expected" : 15
  } ]
}, {
  "object" : "Employee(id=2, name=Mambo Jumbo, salary=9000.0)",
  "fieldValidationSummaries" : [ {
    "fieldName" : "Employee name",
    "validationType" : "MIN_SIZE",
    "actual" : 11,
    "expected" : 15
  } ]
} ]
⚠️ **GitHub.com Fallback** ⚠️