Jmeter + Junit

package com.test.junit;


import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JmeterJunit {
    public String urlPath = "http://www.mocky.io/v2/572ee171120000332519491b";
    public HttpURLConnection connection = null;
    public OutputStream output = null;
    public BufferedReader reader = null;
    public static String request = "{ "type": "getName"}";

    @Before
    public void setUp() throws Exception {
        URL url = new URL(urlPath);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-type", "application/json");
        connection.setRequestProperty("Content-encoding", "UTF-8");
        connection.setRequestProperty("Accept", "application/json");
        connection.connect();
    }

    @After
    public void tearDown() throws Exception {
        if (null != output) {
            try {
                output.close();
            } catch (IOException e) {
                throw e;
            }
        }
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                throw e;
            }
        }
        if (null != connection) {
            connection.disconnect();
        }
    }
    
    public void sendRequest(String request) throws Exception {
        output = connection.getOutputStream();
        output.write(request.getBytes());
        output.flush();
    }

    public String getResponse() throws Exception {
        reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String st;
        StringBuffer stb = new StringBuffer();
        while (null != (st = reader.readLine())) {
            stb.append(st);
        }
        return stb.toString();
    }

    @SuppressWarnings("deprecation")
    public void checkResponse(String response,String name) throws Exception {
        boolean actualResult = response.contains(name);
        assertTrue("the expected status is " + name
                + ", but now it's not", actualResult);
    }

    @Test
    public void test1() throws Exception {
        sendRequest(request);
        checkResponse(getResponse(),"Brett");
    }
    
    @Test
    public void test2() throws Exception {
        sendRequest(request);
        checkResponse(getResponse(),"Jason");
    }
    
    @Test
    public void test3() throws Exception {
        sendRequest(request);
        checkResponse(getResponse(),"Elliotte");
    }
    
    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金枝玉叶9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值