Reputation: 9208
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
Reputation: 1484
If I am understanding your requirement correctly, then:
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
Upvotes: 2