1

This question already has an answer here:

My primary focus will be to make API Calls and to verify Status Code and Json response.If you know any library which makes this task easier please answer. As of now I am using

  1. Postman-Newman
  2. Apache HTTPClient
  3. Jackson & Gson Parser
  4. RestAssured

marked as duplicate by dzieciou, Bharat Mane, IAmMilinPatel, alecxe, Yu Zhang Jul 4 '17 at 20:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Postman-Newman is command-line tool but you mentioned java as your preferred language. Does it mean you don't care about the language? – dzieciou Jul 3 '17 at 12:02
3

If you are looking to verify JSON response, there is a library called postman-bdd for Postman, which lets you write BDD styled test, using the chai-mocha syntax (but can also be used to write jasmine styled syntax). I have used this library for almost 2 months now and it's pretty handy.

Taken directly from the library's npm page -

Postman's built-in test framework uses a boolean-flag syntax for testing, like this:

tests['The correct response code was returned'] = responseCode.code === 200;

tests['The Location header is set'] = postman.getResponseHeader('Location');

tests['The Content-Type is JSON'] = postman.getResponseHeader('Content-Type') === 'application/json';
tests['The response has an ID property'] = JSON.parse(responseBody).id = 12345;

But Postman BDD allows you to use BDD syntax to structure your tests and fluent Chai-JS syntax to write assertions. So the above test suite could look like this instead:

describe('Get customer info', () => {

 it('should return a 200 response', () => {
    response.should.have.status(200);
 });

 it('should set the Location header', () => {
    response.should.have.header('Location');
 });

 it('should return a JSON response', () => {
   response.should.be.json;
 });

 it('should return the correct customer', () => {
  response.body.should.have.property('id', 12345);
 });

});

Apart from this, you can use this library to create custom assertions, using a plugin globals.myCustomAssertions, and then use it in your tests.

What more, you can define before and after hooks that you can use in your API testing, to have something done only once pre or post the test-suite.

2
  • I would recommend all of you to check OpenFeign. It is a cool library with fluent api. Easy to use and very flexible. OpenFeign is a good competitor of the above mentioned Java Rest libraries. – zappee Sep 11 '17 at 7:44
  • @zappee What's the advantage of OpenFeign over Retrofit? – dzieciou Sep 11 '17 at 8:09
  • Please check this topic on stackoverflow.com: stackoverflow.com/questions/46206724/… – zappee Sep 14 '17 at 7:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.