接口單元測試

HttpClient + Junitjava

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.cjhx.risk.common.model.RequestRecordDetailModel;
import com.cjhx.risk.common.model.RequestRecordModel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(SpringJUnit4ClassRunner.class)
public class RestfulUnitTest extends AbstractJUnit4SpringContextTests{
	

	private HttpClient httpClient;

	@SuppressWarnings("deprecation")
	@Test
	public void testRiskInterface(){
		String urlStr = "http://localhost:8087/RiskControl/CheckRisk/0.0.1/Accounts/001";
		httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost(urlStr);
		HttpResponse response = null;
		
		RequestRecordModel model;
		try {
			model = new RequestRecordModel();
			model.setClOrdId("10054");
			model.setIsSync("0");
			model.setRequestId(null);
			model.setInstructionNo(null);
			model.setCallPoint("2");
			model.setCallSys("0");
			model.setBusinessClass("D");
			model.setBeginDate("20170522");
			model.setEndDate("20170522");
			model.setTradeDate(null);
			model.setPurposeType("4");
			model.setFutSettDate(null);
			model.setFutSettDate2(null);
			model.setFixFlag("1");
			model.setTrialFlag("1");
			model.setCheckRangeRiskFlag("1");
			model.setCheckRatioRiskFlag("1");
			model.setTempriskNo(null);
			model.setSenderSubId("1002");
			model.setProcessType(null);
			model.setGroup("1");
			model.setIsO32Order("1");
			model.setWaitingO32("7");
			
			List<RequestRecordDetailModel> detail = new ArrayList<>();
			RequestRecordDetailModel d1 = new RequestRecordDetailModel();
			d1.setFundCode("001199");
			d1.setFundId("3033");
			d1.setCombinNo("3033_000");
			d1.setSymbol("145068");
			d1.setExDestination("1");
			d1.setStype("2");
			d1.setCurrencyCode("CNY");
			d1.setInvestType("1");
			d1.setBusinType("K");
			d1.setStockTargetType("1");
			d1.setPrice("100");
			d1.setOrderQty("5");
			d1.setIsInquiry("0");
			
			detail.add(d1);
			model.setRecordDetails(detail);
			
			String json = new ObjectMapper().writeValueAsString(model);
			StringEntity entity = new StringEntity(json != null ? json : "","utf-8");
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/json");
			
			method.setEntity(entity);
			method.setHeader(new BasicHeader("Accept", "application/json"));
			method.setHeader(new BasicHeader("Accept-Language", "en"));
			
			response = httpClient.execute(method);
		} catch (UnsupportedCharsetException e1) {
			e1.printStackTrace();
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nowglobal.app.jobnow.website.web.dto.IDRequest;
import com.nowglobal.app.jobnow.website.web.dto.TypeRequest;
import com.nowglobal.app.jobnow.website.web.dto.request.CheckJobFilterRequest;
public class CheckTest {

    //接口地址
    private static final String apiURL = "http://localhost:8080/jobnow-managementsite/checkJob";
	public static void main(String args[]){
		try {
			CheckTest login=new CheckTest();
			login.list();
//			login.get();
			
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

    public void list() throws JsonProcessingException{
    		CheckJobFilterRequest request=new CheckJobFilterRequest();
    		request.setPageSize(2);
    		request.setStart(1);
    		request.setType("1");
    		request.setWords("三");
        	String json=new ObjectMapper().writeValueAsString(request);
//        	String str = this.send(null, apiURL + "login/loginTest");
        	String str = this.sendPost(json, apiURL + "/list");
        	System.out.println(str);
        
    }

    private String sendPost(String jsonParam, String path) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(path);
        HttpResponse response = null;
        try {
            StringEntity entity = new StringEntity(jsonParam != null ? jsonParam : "", "utf-8");//解決中文亂碼問題
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            method.setEntity(entity);
            method.setHeader(new BasicHeader("Accept", "application/json"));
            method.setHeader(new BasicHeader("Accept-Language", "en"));
            response = httpClient.execute(method);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        String json = "";
        if (response != null && response.getStatusLine().getStatusCode() == 200) {
            for (Header head : response.getAllHeaders()) {
                System.out.println(head);
            }
            HttpEntity responseEntity = response.getEntity();
            try {
                json = EntityUtils.toString(responseEntity,
                        Charset.forName("UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else{
        	json= response.getStatusLine().getStatusCode()+ response.getStatusLine().getReasonPhrase();
        }
        return json;
    }

    private String sendGet(String path) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet method = new HttpGet(path);
        HttpResponse response = null;
        try {
            response = httpClient.execute(method);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        String json = "";
        if (response != null && response.getStatusLine().getStatusCode() == 200) {
            for (Header head : response.getAllHeaders()) {
                System.out.println(head);
            }
            HttpEntity responseEntity = response.getEntity();
            try {
                json = EntityUtils.toString(responseEntity,
                        Charset.forName("UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else{
        	json= response.getStatusLine().getStatusCode()+ response.getStatusLine().getReasonPhrase();
        }
        return json;
    }

    
}
相關文章
相關標籤/搜索