小编Gab*_*993的帖子

Java Junit测试HTTP POST请求

我需要测试以下方法而不改变方法本身.该方法向服务器发出POST方法.但我需要创建一个独立于服务器的测试用例.

我在将其重定向到本地文件之前测试了类似的方法.但那是因为我把协议作为文件,主机名作为localhost,端口作为-1.

我的问题是这个方法做了一个帖子并转换为HttpURLConnection和wr = new DataOutputStream(conn.getOutputStream()); 不会通过http在本地txt文件上工作.

//构造函数

public HTTPConnector(String usr, String pwd, String protocol,
            String hostname, int port) {
        this.usr = usr;
        this.pwd = pwd;
        this.protocol = protocol;
        this.hostname = hostname;
        this.port = port;

        // connect();
    }
Run Code Online (Sandbox Code Playgroud)

//我需要测试的方法

public String doPost(String reference, String data) throws IOException {
        URL url = null;
        HttpURLConnection conn = null;
        BufferedReader rd = null;
        DataOutputStream wr = null;
        InputStream is = null;
        String line = null;
        StringBuffer response = null;

        url = new URL(protocol, …
Run Code Online (Sandbox Code Playgroud)

java testing post junit http

12
推荐指数
2
解决办法
3万
查看次数

标签 统计

http ×1

java ×1

junit ×1

post ×1

testing ×1