Skip to content

How To Test Spring Boot Application Using Postman

API Testing Blog

How to Test Your Spring Boot Application with Postman

Postman is a powerful tool that can be used to test the REST APIs created with Spring Boot. It provides a user-friendly interface for sending HTTP requests, inspecting responses, and managing API collections. This guide will walk you through the process of testing a Spring Boot application using Postman, providing practical examples and step-by-step instructions.

Setting Up Your Spring Boot Application

Before we dive into Postman, ensure you have a Spring Boot application ready for testing. Your application should expose REST endpoints for users to interact with. If you don’t have one, you can quickly create a basic application. Here’s a simple example:

Sample Spring Boot Code

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}

Run this application to make sure it’s running and accessible.

Getting Started with Postman

  1. Download and Install: If you haven’t already, download Postman from https://www.postman.com/. Postman offers both free and paid plans, and the free plan will suffice for our purposes.

  2. Create a New Request: Start by creating a new request in Postman. Click on the “New” button (the plus icon) and select “Request”.

  3. Setting up the Request:

    • Method: Select the HTTP method (GET, POST, PUT, DELETE, etc.) that your endpoint requires.
    • URL: Enter the complete URL of your endpoint. For example: http://localhost:8080/hello (assuming your application is running on port 8080).

Testing Your Spring Boot Endpoints

Let’s test our sample hello endpoint from the previous example.

  1. Send the Request: Click the “Send” button to execute the request.

  2. Inspect the Response: Postman displays the response within the “Body” tab. In our case, you should see the text “Hello, World!“.

  3. Validating the Response: You can verify the response status code (usually 200 for successful requests) and the response content. Postman offers various tools for inspecting and validating responses, including:

    • Headers: Examine the response headers.
    • Body: View the raw response content or use parsers for JSON, XML, and other formats.
    • Tests: Write assertions and checks using Postman’s built-in testing framework.

How to Test Different Types of Requests

GET Requests:

  • For retrieving data, GET requests are commonly used.
  • In Postman, simply select the “GET” method and enter the endpoint URL.

POST Requests:

  • For creating resources, POST requests come into play.
  • You need to specify the content type in the “Headers” tab (e.g., “application/json”) and define the request body using a ‘Body’ tab option such as ‘raw’, ‘form-data’, or ‘binary’.

PUT Requests:

  • For updating existing resources, PUT requests are used.
  • Like POST requests, you need to specify the content type and provide a request body.

DELETE Requests:

  • For deleting resources, DELETE requests are employed.
  • No request body is needed for a DELETE request, just the endpoint URL.

Example: POST Request with a JSON Payload

Suppose you have an endpoint to create a user.

Sample Spring Boot Code

package com.example.demo;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody User user) {
System.out.println("Creating user: " + user.getName());
return new ResponseEntity<>(user, HttpStatus.CREATED);
}
static class User {
private String name;
public User(@JsonProperty("name") String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}

Postman Configuration

  1. Method: Select “POST”.
  2. URL: http://localhost:8080/users
  3. Headers:
    • Content-Type: application/json
  4. Body:
    • Raw:
      {
      "name": "John Doe"
      }

Send the request, and you should receive a “Created” status code (201) along with the created user in the response.

How to Test Spring Boot Application Using Postman Collections

Postman Collections allow you to organize your API requests, making it easier to test a sequence of API calls or automate your testing workflow.

  1. Create a Collection: Click the “Collections” tab, then the “Create Collection” button. Give your collection a name (e.g., “Spring Boot API Tests”).

  2. Add Requests to the Collection:

    • Right-click on your collection and select “Add Request”.
    • Choose a name for the request and paste the URL of your endpoint.
    • You can customize the request method, headers, and body as needed.
  3. Run the Collection:

    • Click the “Run” button in the collection menu.
    • Postman will execute all the requests in the collection in the order you’ve defined.
  4. Add Test Assertions (Advanced): Postman’s built-in testing framework lets you add assertions to verify the outcome of your API calls.

    • Click the “Tests” tab in a request.
    • Use JavaScript code to write assertions.
    • For example, to check the status code:
      pm.test("Status code is 200", function () {
      pm.response.to.have.status(200);
      });

Tips for Effective Testing

  • Authentication: If your API requires authentication, use Postman to send authorization headers (e.g., Basic Auth, OAuth) with your API calls.
  • Environments: Postman allows you to define environments. This is helpful for setting up different base URLs or API keys used in testing against various environments (development, staging, production).
  • Data Mocking: For situations where you need to control the response data, you can use mocking tools like WireMock with Postman to simulate the behavior of your application’s backend.
  • Automate Testing: With Postman’s collection runner and its scripting capabilities, you can automate tests and integrate them into your CI/CD pipeline.

By following these steps and exploring Postman’s advanced features, you can effectively test your Spring Boot applications with Postman, ensuring your APIs are robust and functioning as intended.

API Testing Blog