JUnit Interface Test

Source: Internet
Author: User

The method used

 Packageinterfacetest_1;ImportOrg.apache.http.HttpResponse;Importorg.apache.http.client.ClientProtocolException;Importorg.apache.http.client.HttpClient;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.entity.StringEntity;Importorg.apache.http.impl.client.DefaultHttpClient;ImportOrg.apache.http.message.BasicNameValuePair;Importorg.apache.http.util.EntityUtils;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;Importjava.util.List;/*** Created by Ethan on 2015/5/12.*/ Public classHttputil { Public Staticstring Postjson (string uri, string params) {HttpClient HttpClient=Newdefaulthttpclient (); String Backstr=NULL; Try{httppost Postmethod=NewHttpPost (URI); Postmethod.setentity (NewStringentity (params, "utf-8"));//Hao Ricoh 弬 indent Board ~ å ostPostmethod.setheader ("Content-type", "Application/json; Charset=utf-8 "); HttpResponse Response= Httpclient.execute (Postmethod);//gallium цpost à regulation 硶Backstr = entityutils.tostring (Response.getentity (), "Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(clientprotocolexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returnBackstr; }     Public StaticString Dopost (String uri, list<basicnamevaluepair>params) {HttpClient HttpClient=Newdefaulthttpclient (); String Backstr=NULL; Try{httppost Postmethod=NewHttpPost (URI); Postmethod.setentity (NewUrlencodedformentity (params, "utf-8"));//Hao Ricoh 弬 indent Board ~ å ost//entity Juan?Postmethod.setheader ("Content-type",                    "Application/x-www-form-urlencoded; Charset=utf-8 "); HttpResponse Response= Httpclient.execute (Postmethod);//gallium цpost à regulation 硶Backstr = entityutils.tostring (Response.getentity (), "Utf-8"); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(clientprotocolexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnBackstr; }}


Return package Processing

 PackageVo; Public classRESVO {PrivateString data; PrivateInteger result; PrivateString msg;  PublicString GetData () {returndata; }     Public voidsetData (String data) { This. data =data; }     PublicInteger GetResult () {returnresult; }     Public voidSetresult (Integer result) { This. result =result; }     PublicString getmsg () {returnmsg; }     Public voidsetmsg (String msg) { This. msg =msg; }    }

Upload test in JSON format

 Packagejunittest;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.junit.Test;ImportCom.alibaba.fastjson.JSON;ImportVO.RESVO;ImportInterfacetest_1.httputil; Public classXXX {//........@Test Public voidSaveuserratio () {intGroupid=7; intBabyid = 5; String website= "Http://192.168.1.109/iftest/xxx.htm"; Map<String,Object> m =NewHashmap<string,object>(); Map<String,Object> A =NewHashmap<string,object>(); M.put ("GroupId", groupId); A.put ("Babyid", Babyid); M.put (A, a); String Res=Httputil.postjson (Website,json.tojsonstring (m));   System.out.println (Json.tojsonstring (m)); RESVO RESVO= Json.parseobject (RES,RESVO.class);   System.out.println (RES); Org.junit.Assert.assertSame (0, Resvo.getresult ()); }


Interface needs to upload pictures of the test

 Packagejunittest;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportOrg.apache.http.message.BasicNameValuePair;Importorg.junit.Test;ImportCom.alibaba.fastjson.JSON;ImportVO.RESVO;ImportInterfacetest_1.httputil; Public classnotestest {@Test Public voidNotessave () {String groupId= "7"; String Picturestatus= "0"; String Pictures= "1"; String website= "Http://192.168.1.101:8080/if/note.htm"; Map<String,Object> m =NewHashmap<string,object>(); List<BasicNameValuePair> params =NewArraylist<basicnamevaluepair>(); Params.add (NewBasicnamevaluepair ("GroupId", groupId)); Params.add (NewBasicnamevaluepair ("Note.picturestatus", Picturestatus)); Params.add (NewBasicnamevaluepair ("Note.pictures", pictures));        System.out.println (json.tojsonstring (params)); String Res=Httputil.dopost (Website,params); RESVO RESVO= Json.parseobject (RES,RESVO.class);        System.out.println (RES); Org.junit.Assert.assertSame (0, Resvo.getresult ()); }}

JUnit Interface Test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.