首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何运行这个java文件?

如何运行这个java文件?
EN

Stack Overflow用户
提问于 2020-10-06 07:11:06
回答 2查看 80关注 0票数 0

我被告知要执行这个链接到一个虚拟号码的java程序,当你运行它时,输出的是一个号码代码,或者是我在短信中发送的号码。

我正在使用selenium和maven,还在eclipse程序中使用,并被告知要使用testng插件。他们要求我在.xml文件中使用@test注释来执行它。

我知道编程,老实说,我不知道如何写xml文件来运行这个,当我问告诉我做这件事的人时,他们所做的一切都是说谷歌它,我试过了,但我没有找到任何东西。

代码是:

代码语言:javascript
复制
package utility;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
import org.apache.commons.codec.binary.Base64;
//import sun.misc.BASE64Encoder;

public class APIcall {

    // String MobileNo = ""; // virtual mobile no. (From message media)
    private final String USER_AGENT = "Mozilla/5.0";

    // Msg media Account: 
    // Password: 
    String name = ;
    String password = ;
    String authString = name + ":" + password;
    //String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
    String authStringEnc = new Base64().encodeBase64String(authString.getBytes());

    static String URL = "https://api.messagemedia.com";
    String getURI = "/v1/replies";

    static String postURI = "/v1/replies/confirmed";
    static Client client = ClientBuilder.newClient();

    public Data getRequest() {
        Response response = (Response) client.target(URL + 
                     getURI).request(MediaType.APPLICATION_JSON_TYPE)
                     .header("Authorization", "Basic " + authStringEnc).get();
        String body = response.readEntity(String.class);
        // System.out.println("status: " + response.getStatus());
        // System.out.println("headers \n: " + response.getHeaders());
        System.out.println("body: \n" + body);

        JSONObject json = new JSONObject(body);
        JSONArray reply = (JSONArray) json.get("replies");

        List<String> ids = new ArrayList<String>();
        for (int idx = 0; idx < reply.length(); idx++) {
            // ids.add(replyIds.get(idx).toString());
            ids.add(reply.getJSONObject(idx).getString("reply_id"));
        }

        Assert.assertEquals(200, response.getStatus());

        /*
         * String OTPcode = (String) body.substring(body.lastIndexOf(' ') + 1); OTPcode
         * = OTPcode.substring(0, 6).trim();
         */

         String getContent = reply.getJSONObject(0).getString("content");
         System.out.println(getContent);
         String[] content = getContent.split(" ");
         String code = content[content.length - 1];
         // System.out.println("OTP code : " + code);
         // System.out.println("List of replies : " + ids);

         return new Data(code, ids, getContent);

         //JSONException
    }

    public final class Data {

        public String otp;
        public List<String> list;
        public String getContent;

        public Data(String otp, List list, String getContent) {
            this.otp = otp;
            this.list = list;
            this.getContent = getContent;
       }

    }

    public void postRequest() {
        /*
         * Post request : Add reply ids recieved from GET request and pass it in body
         * with POST request to confirm replies
         */
         List<String> ids = getRequest().list;
         String postStr = "{ \"reply_ids\":[";
         for (String id : ids) {
             postStr += "\"" + id + "\", ";
         }
         postStr += " ]}";

         StringBuilder postString = new StringBuilder(postStr);
         postString.replace(postStr.lastIndexOf(","), 
                            postStr.lastIndexOf(",") + 1, "");
         postStr = postString.toString();

         // System.out.println("Reply id list : " + postStr);
         Response response = client.target(URL + postURI)
                  .request(MediaType.APPLICATION_JSON_TYPE)
                  .header("Accept", "application/json")
                  .header("Authorization", "Basic " + 
                  authStringEnc).header("Content-Type", 
                  "application/json").post(Entity.json(postStr));

         /*
          * System.out.println("status: " + response.getStatus());
          * System.out.println("headers \n: " + response.getHeaders());
          * System.out.println("body: \n" + 
                               response.readEntity(String.class));
          */
      }

      private void sendGet() throws Exception {
          String url = URL + getURI;
          URL obj = new URL(url);
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();

          // optional default is GET
          con.setRequestMethod("GET");

          // add request header
          con.setRequestProperty("User-Agent", USER_AGENT);
          con.setRequestProperty("Accept", "application/json");
          con.setRequestProperty("Authorization", "Basic " + authStringEnc);

          int responseCode = con.getResponseCode();
          System.out.println("\nSending 'GET' request to URL : " + url);
          System.out.println("Response Code : " + responseCode);

          BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
          String inputLine;
          StringBuffer response = new StringBuffer();
          while ((inputLine = in.readLine()) != null) {
               response.append(inputLine);
          }
          in.close();

          // print result
          System.out.println(response.toString());
      }

      private void sendPost() throws Exception {
          String url = URL + postURI;
          URL obj = new URL(url);
          HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

         // add reuqest header
         con.setRequestMethod("POST");
         con.setRequestProperty("User-Agent", USER_AGENT);
         // con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
         con.setRequestProperty("Accept", "application/json");
         con.setRequestProperty("Authorization", "Basic " + authStringEnc);

         String urlParameters = "{\"reply_ids\":\"25ebbf01-5614-4ea6-a4f7-67b752d18ed2\",\"63ed8e18-ecf0-444d-b79e-341e944b0b94\"}";

         // Send post request
         con.setDoOutput(true);
         DataOutputStream wr = new DataOutputStream(con.getOutputStream());

         wr.writeBytes(urlParameters);
         wr.flush();
         wr.close();

         int responseCode = con.getResponseCode();
         System.out.println("\nSending 'POST' request to URL : " + url);
         System.out.println("Post parameters : " + urlParameters);
         System.out.println("Response Code : " + responseCode);

         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
         String inputLine;
         StringBuffer response = new StringBuffer();
         while ((inputLine = in.readLine()) != null) {
             response.append(inputLine);
         }
         in.close();

         // print result
         System.out.println(response.toString());
     }

     public static void main(String[] args) throws Exception {
         /*
          * System.setProperty("http.proxyHost", "");
          * System.setProperty("http.proxyPort", "");
          */
         APIcall apiCall = new APIcall();
         apiCall.getRequest();
         apiCall.postRequest();
     }   

}
EN

回答 2

Stack Overflow用户

发布于 2020-10-07 17:27:38

@Test注解的行为类似于每个测试的void main (这是一个带有@Test注解的方法)。您可以在maven pom.xml文件中为junit或testng添加一个依赖项(它们中的任何一个都会带来这个注释),然后,您可以通过从集成开发环境运行该方法来触发测试,或者从命令行中简单地使用:

代码语言:javascript
复制
mvn clean test   - to trigger all existent tests

mvn clean test -Dtest=your.package.TestClassName   - to trigger tests from a class
mvn clean test -Dtest=your.package.TestClassName#particularMethod   - to trigger a specific test
票数 0
EN

Stack Overflow用户

发布于 2020-10-08 02:46:51

如果我没理解错的话,他们希望你把这个转换成TestNG测试,这应该不会太难,因为测试和方法已经构建好了。考虑到你既不懂Java,也不懂编程,我建议你看看Youtube上的几个视频,让你快速掌握TestNG。我相信一旦你接触了TestNG,大部分代码以及如何转换它就会变得更有意义。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64217345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档