Testing Endpoints¶
According to Wikipedia
Quote
API testing is a type of software testing that involves testing application programming interfaces (APIs) directly and as part of integration testing to determine if they meet expectations for functionality, reliability, performance, and security
When it comes to testing API endpoints, you can do this manually or automatically. Postman offers functionalities for both. There are also Java libraries that assist you with API testing. I personally prefer to use both Postman and write Java code for Functional Testing (to make sure we have status, the correct JSON response, etc).
Unirest¶
You can use use a lightweight HTTP client library called Unirest for testing your API in Java. Add the following to the dependencies
clause of build.gradle
:
1 | compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9' |
Here is a simple test1 that checks the right status is returned for a get request:
1 2 3 4 5 6 7 8 9 | @Test public void getCoursesRequestReturns200() throws UnirestException { HttpResponse<JsonNode> jsonResponse = Unirest.get("http://127.0.0.1:7000/courses") .asJson(); assertEquals(200, jsonResponse.getStatus()); } |
I will provide a working sample of tests written using Unirest in homework 3!
-
The server must be running when you run this test. ↩