Skip to content

REST Assured Basics - Lab 3

1. Create the package to add the restrictions tests

  1. Create an additional package called simulation in com.workshop package in the src/test/java folder

2. Retrieve all records from the Simulation API

Steps

  1. Create a Java class named SimulationsTest in com.workshop.simulation in the src/test/java folder
  2. Make SimulationsTest extends BaseApiConfiguration
  3. Create a test method named shouldRetrieveAllSimulations()
  4. Add the following to the test:
    • action (when()) to get (get()) the /simulations/ endpoint
    • assert (then()) in the status code expecting HTTP 200
      • tip: use HttpStatus.SC_OK
      • add a body() assertion in the response body for each attribute using CoreMatchers.is() to validate values returned
  5. Run the test

Tips

  • don't forget to set the array position [0].
  • for the id attribute use the CoreMatchers.notNullValue() assertion
  • for the amount attribute, use the value as BigDecimal
  • example: new BigDecimal("11000.00")

Expected results

  • Green test execution where the following verifications will be performed successfully
    • status code
    • assertion in all the attributes

Solution

Click to see...
@Test
void shouldRetrieveAllSimulations() {
    when()
        .get("/simulations/")
    .then()
        .statusCode(HttpStatus.SC_OK)
        .body("[0].id", CoreMatchers.notNullValue())
        .body("[0].name", CoreMatchers.is("Tom"))
        .body("[0].cpf", CoreMatchers.is("66414919004"))
        .body("[0].email", CoreMatchers.is("tom@gmail.com"))
        .body("[0].amount", CoreMatchers.is(new BigDecimal("11000.00")))
        .body("[0].installments", CoreMatchers.is(3))
        .body("[0].insurance", CoreMatchers.is(true));
}  

3. Retrieve all records and assert their size

Steps

  1. In the SimulationsTest class, create a test method named shouldRetrieveAllSimulationsCheckingSize()
  2. Add the same actions from the previous test, making a GET request to /simulations/
  3. In the assertion, use $ instead of an attribute name and use Matchers.hasSize() to assert the number of elements returned
  4. Run the test

Expected results

  • Green test execution where the following verifications will be performed successfully
    • status code
    • assertion in the array size

Solution

Click to see...
@Test
void shouldRetrieveAllSimulationsCheckingSize() {
    when()
        .get("/simulations/")
    .then()
        .statusCode(HttpStatus.SC_OK)
        .body("$", Matchers.hasSize(2));
} 

4. Using soft assertions

Steps

  1. In the SimulationsTest class, modify the shouldRetrieveAllSimulations() to have the soft assertions approach
  2. Make a temporary change in the following attribute values
  3. name to Unknown
  4. installments to 0
  5. Run the test

Expected results

  • The test execution will fail with the following error in the console
    java.lang.AssertionError: 2 expectations failed.
    JSON path [0].name doesn't match.
    Expected: is "Unknown"
      Actual: Tom
    
    JSON path [0].installments doesn't match.
    Expected: is <0>
      Actual: <3>
    
  • Rever the data changes