Generative AI at work
2 min read

In today’s tutorial, we are going to leverage Postman framework to successfully test RESTful Web Services.

We will also discuss a simple JUnit test case, which is calling the getAllUsers method in userService. We can check the following code:



public class UserTests {


UserService userSevice;


public void testAllUsers(){

List<User> users = userSevice.getAllUsers();

assertEquals(3, users.size());



In the preceding code, we have called getAllUsers and verified the total count. Let’s test the single-user method in another test case:

// other methods


public void testSingleUser(){

User user = userSevice.getUser(100);



In the preceding code snippets, we just tested our service layer and verified the business logic. However, we can directly test the controller by using mocking methods.


First, we shall start with a simple API for getting all the users:


Generative AI at work

The earlier method will get all the users. The Postman screenshot for getting all the users is as follows:

Postman screenshot

In the preceding screenshot, we can see that we get all the users that we added before. We have used the GET method to call this API.

Adding a user – Postman

Let’s try to use the POST method in user to add a new user:


Add the user, as shown in the following screenshot:

Add user

In the preceding result, we can see the JSON output:


"result" : "added"


Generating a JWT – Postman

Let’s try generating the token (JWT) by calling the generate token API in Postman using the following code:


We can clearly see that we use subject in the Body to generate the token. Once we call the API, we will get the token. We can check the token in the following screenshot:

Generate JWT

Getting the subject from the token

By using the existing token that we created before, we will get the subject by calling the get subject API:


The result will be as shown in the following screenshot:

Subject from token

In the preceding API call, we sent the token in the API to get the subject. We can see the subject in the resulting JSON.

You read an excerpt from Building RESTful Web Services with Spring 5 – Second Edition written by Raja CSP Raman.  From this book, you will learn to build resilient software in Java with the help of the Spring 5.0 framework.

Check out the other tutorials from this book:

More Spring 5 tutorials:


Generative AI at work


Please enter your comment!
Please enter your name here