简体   繁体   English

使用Java和JSON请求进行服务器端自动化测试

[英]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. 我之前没有做过自动化测试,只有jUnit测试,现在我有一个请求。 But the automation is not on frontend using Selenium or so, it is simpler than that, using JSONs requests. 但是使用Selenium左右的自动化不在前端,它比使用JSON请求更简单。 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. 正如您所说的JSON,您可以使用JAVA轻松完成此操作。 Use java package - org.json.JSONObject to create request and validate JSON format. 使用java包 - org.json.JSONObject创建请求并验证JSON格式。
  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; 使用包:import org.apache.http.impl.client.CloseableHttpClient;

    you may need to download apache - httpClient jar also. 你可能还需要下载apache - httpClient jar。

  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. 最后你有字符串响应,这可以通过使用java package - org.json.JSONObject进一步分析。 Analyse the JSON response like: 分析JSON响应,如:

     JSONObject js = new JSONObject(hudsonRTBObjectJSONString); 

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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