Denees
Denees

Reputation: 9208

Server side automation testing using Java and JSON requests

I have not done automation testing before, only jUnit testing, now I have a request to do that. But the automation is not on frontend using Selenium or so, it is simpler than that, using JSONs requests. I understand the principles of that, but don't know how to do it programmatically correct. I have to do some payment request to the server and see if the response is correct, not just the code, but the details of response as well.

So far I have done the part with request to the server, now when I receive the response, what is the best way to compare or to check it, or how I can see if everything is right? can you point me in the right direction?

Upvotes: 3

Views: 2962

Answers (1)

Pankaj Kumar Katiyar
Pankaj Kumar Katiyar

Reputation: 1484

If I am understanding your requirement correctly, then:

  1. First check what is the format of request and response.
  2. 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.
  3. 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.

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

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

        String line = "";
        StringBuffer result = new StringBuffer();
        while ((line = rd.readLine()) != null)
        {
            result.append(line);
        }
    
  6. 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

Upvotes: 2

Related Questions