vendredi 6 novembre 2015

Integration tests for Rest API

I'd like to get different pointe of views about how to create integration tests for Rest APIs.

The first option would be using cucumber as described in the "The Cucumber Book":

Scenario: Get person
  Given The system knows about the following person:
    | fname | lname | address | zipcode |
    | Luca  | Brow  | 1, Test | 098716  |
  When the client requests GET /person/(\d+)
  Then the response should be JSON:
    """
    {
      "fname": "Luca",
      "lname": "Brow",
      "address": {
        "first": "1, Test",
        "zipcode": "098716"
      }
    }
    """

The second option would be (again) using cucumber, but removing the technical detail as described here:

Scenario: Get person
  Given The system knows about the following person:
    | fname | lname | address | zipcode |
    | Luca  | Brow  | 1, Test | 098716  |
  When the client requests the person
  Then the response contains the following attributes:
    | fname            | Luca    |
    | lname            | Brow    |
    | address :first   | 1, Test |
    | address :zipcode | 098716  |

And the third option would be using Spring as described here:

private MockMvc mockMvc;

@Test
public void findAll() throws Exception {
    mockMvc.perform(get("/person/1"))
            .andExpect(status().isOk())
            .andExpect(content().mimeType(IntegrationTestUtil.APPLICATION_JSON_UTF8))
            .andExpect(jsonPath("$.fname", is("Luca")))
            .andExpect(jsonPath("$.lname", is("Brow")))
            .andExpect(jsonPath("$.address.first", is("1, Test")))
            .andExpect(jsonPath("$.address.zipcode", is("098716")));
}

I really like the second option since it looks cleaner to business users and testers, but on the other hand for a developer that will consume this API the first option looks more visible since it shows the JSON response.

The third option is the easiest one since it's just Java code, but the readability and the cross-team interaction is not as nice as cucumber.

Aucun commentaire:

Enregistrer un commentaire