Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Whenever testing REST APIs, we can either be targetting targeting the client/consumer or the server/producer.

In this tutorial we'll show you how a REST API can be tested using Java and the REST Assured library, together with JUnit and WireMock.

...

REST Assured is a Java library that implements a DSL for in order to easily validating validate REST API based services by abstracting low-level details of HTTP requests and of parsing responses.

It provides a syntax based on Gherkin (i.e. given, when, then) for better readibilityreadability.


From REST Assured documentation, imagine that you have a REST based service accessibile accessible in the http://localhost:8080/lotto/{id} endpoint, and returning a JSON content.

Code Block
languagejs
{
   "lotto":{
      "lottoId":5,
      "winning-numbers":[2,45,34,23,7,5,3],
      "winners":[
         {
            "winnerId":23,
            "numbers":[2,45,34,23,3,5]
         },
         {
            "winnerId":54,
            "numbers":[52,3,12,11,18,22]
         }
      ]
   }
}


A Also ensure that you have a JUnit test to check that the HTTP response code is OK (i.e. 200) and that the "lotto" object returned is correct, . This could be something as follows.

...

Info
titlePlease note
To be clear, in this project, tests are performed against the server provided by WireMock that mocks responses from the API; this was done just be by convenience of the workshop, so it's not dependent on the public server availability. In a real scenario, you would perform tests against the target service or you use WireMock to validate your client/consumer library implementaiton.

...

After successfully running the tests and generating the aggregated JUnit XML report (e.g., merged-test-results.xml), it can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution, or even by using a CI tool of your choice).



Each JUnit 's test is mapped to a Generic Test in Jira, and the Generic Test Definition field contains the name of the package, the class and the method name that implements the Test Case. The summary of each Test issue is filled out with the name of the method corresponding to the JUnit Test.

...

Info
titlePlease note

In the case of data-driven tests, as the JUnit XML report treats each row of the data provider as a different "test case," , then you 'll will end up with several Tests in Xray.

...

  • Multiple runs of your tests can be grouped and consolidate consolidated in a Test Plan, so you can have an updated overview of their current state
  • After importing the results, you can link the corresponding Test issues with an existing requirement or user story and thus truck coverage directly on the respective issue, or even on a Agile board
    •     

...