6

I was testing web services manually using Postman or REST client. I want to automate testing here. I use REST with JSON format.

Questions:

  1. How to use target URL ?

  2. How to pass header values?

  3. How to pass payload ?

Also please provide examples with code, as it will help me a lot . Thanks in advance.

  • Any One know used jersey API , I heard it has got all required components to Automate REST Services . – Jagadeesh Jan 9 '17 at 6:58
4

If you would love to test REST API then I would recommend you to use Rest Assured which is a Java Library.

It’s better to create Page Object Model and use Testing Framework like TestNG to write tests. Along with the main library ( REST ASSURED) that you can use for testing REST API.

Below is the official guide for the same:

rest-assured/rest-assured

OR

You may also follow the step by step tutorial which I have created for REST Assured:

Testing Rest Services using Rest Assured

Thanks,

Ashwin Karangutkar

  • Any one Know OR used "jersey client" I heard it is also one API which can be used for REST Services ? – Jagadeesh Jan 9 '17 at 6:54
  • Ashwin , Ur bolg does not talks about who to Work on POST, PUT , DELETE methods ? – Jagadeesh Mar 24 '17 at 6:24
  • @JagadeeshJ Please go through the entire blog. Seems like you read only one post, – Ashwiinn Karaangutkar Apr 5 '17 at 5:56
2

Karate is a relatively new project that is specialized for testing JSON web-services and it runs on the JVM.

Disclaimer: am dev.

There are a set of demos and examples that will get you started very easily. One of the highlights of Karate is the "native" support for JSON and being able to perform a "deep equals" on two payloads.

1

I think this link might help you.
It is a rest webservice engine written in spring and mybatis I also have a user guide.

  • 1
    Please provide more detailes instead of just a link. – bish Sep 27 '15 at 11:58
0

There are many possible ways to automate 'API Calls', The best automation tools would be JMeter for functional tests and frisby.js for testing API endpoints.

frisby.js is a REST API testing framework built on node.js

The 'Apache JMeter' application, is an open source software, a 100% pure 'Java application' designed to load test functional behavior and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.

However, JMeter API's can be used in java program. here is a JMeter API Documentation for more information on JMeter API, Also Please refer to this thread, to understand how to run an entire test plan programmatically from or within a Java program.

It would be best to create a test plan .JMX file using JMeter GUI and run it in JMeter 'non-GUI' mode.

For creating a test plan in JMeter GUI. you could find more information in this thread. Well this tutorial was very helpful to me for learning to create 'JMeter scripts'.

If you have a basic understanding of javascript, you can try 'frisby.js' referring to this tutorial. I believe both these tools are perfect for API coverage.

  • 1
    From what I can see this answer has nothing to do with Java (which is what the OP requested) – Ardesco Sep 10 '15 at 13:11
  • Sorry for the incomplete information, I have now updated my answer. Thanks. – swapnil.s Sep 10 '15 at 14:58
  • Isn't it Apache JMeter Is more of Load testing Tool ? – Jagadeesh Jan 9 '17 at 7:01
  • JMeter was originally built to provide an open source solution for load and performance testing. However, JMeter can also be used to perform functionality tests on an application's backend. You can also use assertions to configure functional testing in JMeter. – swapnil.s Jan 5 '18 at 13:31
0
If I am understanding your requirement correctly for functional testing, then:

First check what is the format of request and response.
As you said its JSON, you can easily do this using JAVA. Use java package - org.json.JSONObject to create request and validate JSON format.
You can send request and get response by this simple code:

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet GetRequest = new HttpGet("ServerURL");
        CloseableHttpResponse response = httpclient.execute(GetRequest);
use packages: import org.apache.http.impl.client.CloseableHttpClient;

you may need to download apache - httpClient jar also.

Once you have the response, read it using some reader, like:

BufferedReader rd = new BufferedReader( new  InputStreamReader(response.getEntity().getContent()));
Convert this response into a string like:

    String line = "";
    StringBuffer result = new StringBuffer();
    while ((line = rd.readLine()) != null)
    {
        result.append(line);
    }
Finally you have the string response, this can be further analysed by using java package - org.json.JSONObject. Analyse the JSON response like:

JSONObject js = new JSONObject(hudsonRTBObjectJSONString);
Hope this helps
0

Currently, I am using different approach automate rest services.

  1. Converting JSON schema / JSON files to Java Classes with help JSONschema2Pojo.
  2. Using Spring Rest Template to make requests and get Responses as Java Object.

private TestRestTemplate rest = new TestRestTemplate(); ResponseEntity<ResponceObject> response = rest.getForEntity("URL", Request.class);

From here you can get http Headers as
HttpHeaders headers = p.getHeaders();

Response body as -
ResponceObject resBody = response.getbody();

On top of this if We can use cucumber. we can have a lot reusability of code.

This is more Object Oriented way. I strongly disagree creating manual JSON files

More information can be found at.
jsonschema2pojo &
baeldung

I am thinking to create sample Git projects on this.

0

Below is the best way to construct the request body, sample code for constructing request body and hit the service:

String requestbody="";
    requestbody="{"+
        "\"args\": {},"+
        "\"headers\": {"+
        "\"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\","+
        "\"Accept-Encoding\": \"gzip, deflate\","+
        "\"Accept-Language\": \"en-US,en;q=0.9\","+
        "\"Host\": \"yourapihere.com\","+
        "\"Upgrade-Insecure-Requests\": \"1\","+
        "\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36\""+
        "},"+
        "\"method\": \"GET\","+
        "\"origin\": \"103.43.112.97\","+
        "\"url\": \"http://yourapihere.com/\""+
        "}";

HttpClient httpClient =new HttpClient();
PostMethod postMethod=new PostMethod(url);
postMethod.setRequestEntity(new StringRequestEntity(requestBody, null, "UTF-8"));
postMethod.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
        postMethod.setRequestHeader("Accept", "application/json; charset=UTF-8");
int statuscode=httpClient.executeMethod(postMethod);
String responsebody2=postMethod.getResponseBodyAsString();
System.out.println(responsebody2);

for more details, check out this Video.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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