简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM