Skip to content

RESTful services Tests

Ding Ma edited this page Feb 29, 2020 · 1 revision

Dependencies

Technologies used:

  • Postman: for creating the tests
  • Newman: for automating the tests. To install, run npm install -g newman

How do we use it?

Go to here to download the API test JSON file. Two ways to run the tests:
1- Open the project in STS 4 and run the PetShelterApplication.java as a Spring project.
2.a- This file can be either imported into Postman and ran each of them manually.
2.b- The file can be ran with Newman with the following command: newman run APITests.json.

In Postman, we can clearly see the return statements for every API method calls as shown by logging in here. Since Newman is a CLI, we used assertion that detects if the return code and return message is what we expected. For the purpose of the wiki, we will show one Postman example but the rest will be done through Newman.


Test Cases

The tests runs through a potential use-case for an average user. Here is the order that we are going to test our controller.
1- Register user and admin as well as confirming their accounts
2- Forum tests (create, edit, update, get)
3- Donation (create, get)
4- Change user password, reset password from admin
5- Logout
6- Delete User


Postman Example - Registering an user

So here in Postman, we can see that we did a POST operation with the user's credential as the JSON file. UserRegister

If we look at the test tab, we can easily see that it is a simple assert if the return code is 201 and we are setting the user's token as a global variable for latter on. This allows easier testing because we don't have to copy paste ever time - The token changes every login. UserTests

Newman Example - Running the script

After executing the command newman run APITests.json, we will see the test currently being executed and the a check mark to see if the test passed. The following picture shows that those tests passed.
Newman user tests

At the end, we will see an summary of all the tests.
SummaryTests

In the case of a failure, Newman will indicate which tests failed and how did it fail. For instance, if a user registers with a password but fails to enter the right password. The system will not let him login. UserLoginFailure