Testing REST Services with Postman

Postman is a popular tool for testing APIs in a user-friendly graphical environment. You can use Postman to send API requests to any REST API and receive replies. In this article we will learn how to use it to test a JAX-RS Application which exposes CRUD Endpoints

Postman in a nutshell

Firstly, you need to download Postman from https://www.postman.com/downloads/

Install it on your machine. You also need a REST Server. For the purpose of this tutorial, we will run a JAX-RS Application on top of WildFly. The following article discusses in depth the source code for this article: REST CRUD application with JPA | A complete example

Here is in a nutshell the REST Service we are going to test:

@GET
public List<Customer> getAll() {
    return customerRepository.findAll();
}

@POST
public Response create(Customer customer) {
    customerRepository.createCustomer(customer);
    return Response.status(201).build();
}

@PUT
public Response update(Customer customer) {
    customerRepository.updateCustomer(customer);
    return Response.status(204).build();
}
@DELETE
public Response delete(@QueryParam("id") Long customerId) {
    customerRepository.deleteCustomer(customerId);
    return Response.status(204).build();
}

Next, let’s create our Postman project.

Creating your Postman Project

Postman, can organize requests in many ways. You can either start a new HTTP Request and test your application from there. For the purposes of this tutorial, we will create a Postman Collection. To do this, select the collections option:

postman tutorial rest services

Next, click “Create new Collection“, to start the collection creation process. A new tab will open in the center area of UI. Choose a name for the collection , for example: “JAX-RS Crud”.

Now that everything is set up, we’re ready to create a first request within Postman.

Adding CRUD Requests

Firstly, click the plus(+) icon next to the “JAX-RS Crud” Collection. Set the request type to POST and enter http://localhost:8080/jaxrs-demo/rest/customers into the request URL field.

Next, select the Body tab of the request and then change the radio button beneath to “raw“. On the right, there is a dropdown that says “Text”, change it to JSON:

This will configure the request to send and receive information in a JSON format. Next, click on Send and verify that the Status is 201 (Created).

Finally, we need to save this request into our Collection. This will make sure that it picks up the Body or variables that we previously created. To do, this click the “save” button in the top right of the request area.

This will make a dialog window appear that asks you, what you’d like to call the request and where you’d like to save it. You can name it anything you’d like but ensure you select your “JAX-RS Crud” collection in the lower half of the dialog.

Much the same way, you can create the next operation which is a GET. The URL address is the same (http://localhost:8080/jaxrs-demo/rest/customers) , however, we don’t pass any Body contect to the request:

Save the HTTP GET Request in your collection.

Next, we will add a PUT Request to modify the attributes of an element of our List of Customers. Just like the POST request, we will be passing the Customer attributes in JSON format. Make sure you select the PUT Request before sending and saving to your Collection:

Finally, let’s add a DELETE Request. According to our contract, to delete an item we pass the Customer is as Query parameter. You can either add the id directly on the URL, or use the Table Params table below:

Running the Collection of Requests

One advantage of creating a Collection is that you can test all Requests in a batch. From the “JAX-RS Crud” Collection click on the dots (…) and choose Run. You can now execute all Request and choose how many iterations/delay you want to add in your test:

Exporting the Collection

When you have added all Requests and tested them successfully, it’s time to export the Postman collection so that you can share it with your testers. On the left panel, near your “JAX-RS Crud” Collection, click on the dots (…) and choose Export.

This allows you to export the Collection in JSON format which you can then import from Postman Desktop application.

Conclusion
This article was a quick walk through testing JAX-RS applications using Postman. We have learnt how to create a Collection to group the list of Requests in a single batch that you can execute or export.

The source code, which includes the Postman collection, is available here: https://github.com/fmarchioni/mastertheboss/tree/master/jax-rs/crud

Found the article helpful? if so please follow us on Socials