Test Salesforce REST API with Postman

Postman is my favorite tool for testing REST APIs. Workbench is great for ad-hoc testing but I like Postman better because I can create a suite of repeatable, scriptable tests. You can download and install Postman from getpostman.com. When you run it you should see the main screen.

Postman main screen

Prerequisites

In this post we will test the REST API we created in the last few posts. To follow along with this post you can clone the GitHub repo and push the code to a scratch org or start at the beginning of the series if you would like to follow along with building the API.

Before we can make our first request we need some information about our scratch org. From the VS Code command palette select SFDX: Display Org Details for Default Org .

  1. Copy the Access Token from VS Code
  2. Click Authorization in Postman.
  3. Select OAuth 2.0 for the authorization type
  4. Select Request Headers
  5. Paste the token into the text box.
  6. Copy the Instance Url from VS Code.
  7. Paste the Url into Postman.

Next we can setup the rest of our request. Let’s start with a POST request to add a House to the database. This will be the body of our request.

  1. In Postman change the request type from GET to POST.
  2. Append the following to the Base Url: /services/apexrest/House
  3. Select the Body tab.
  4. Select raw and JSON
  5. Paste the JSON below into the body.
  6. Click Send

If everything worked the status code should be 201 Created and the response should be the Id of the newly created object.

Add a Test

  1. Click on the test tab
  2. Paste the following JavaScript into the text area

This is a simple test that will pass if the POST method returns 201 and fail if it doesn’t. Click Send to send the request and click the Test Results tab to see the result.

Postman test result

Create a test suite with Postman collections

In Postman click New, then Collection, then give your collection a name and hit Create. I named mine House Test Suite.

Now click Save As to save your request. Give your request a name. I called mine Post returns 201. Select the House Test suite collection and click Save to House Test Suite.

You now have a Postman collection that you can execute as a test suite for the House API. It’s only one test but we can easily add requests and test for the positive and negative cases for all four HTTP methods and have a complete test suite.

To run the test suite click the Runner button in Postman. This will open the Collection Runner window. Select the House Test Suite then click the Run House Tests Suite button*.* This will run the collection and show the results.

Conclusion

Postman has a rich set of features and we have just scratched the surface. For example, you can build more sophisticated test suites where the results of one test are used in subsequent tests. You can also use Newman, the Postman command line tool, to script the execution of test suites and make them part of your CI/CD process.

Further Reading

Leave a Comment