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.

Upvotes: 0

Views: 700

Answers (2)

Biraj B Choudhury
Biraj B Choudhury

Reputation: 684

You should use the third option but not with junit, you should do it using spock.This is the best of both the worlds.

Spock tests are written like this

def "description of what you want to test"() {
    given:
        //Do what is pre-requisite to the test

    when:
        def response = mockMvc.perform(get("/person/id")).andReturn().getResponse();

    then:  
        checkForResponse.each {
        c->c(response )

    }

    where:
        id      | checkResponse
        1       | [ResponseChecker.correctPersondetails()]
        100     | [ResponseChecker.incorrectPersondetails()]

  }

Upvotes: 1

victor gallet
victor gallet

Reputation: 1898

Integration test are made to test if components of your application can work together. For example, you test some requests to database and mvc controller with integration tests. Integration tests are here to test your infrastructure.

On the other hand, BDD tests are made to facilitate communication between development and specifications. The common idea is to write tests or specification by example. There are definitely not design to write integration tests.

I would recommend the third option.

Upvotes: 0

Related Questions