天天看點

對web項目的單元測試方法

1、httpclient模拟請求,需要打開伺服器

/*

map<string,string>param=newhashmap<string,string>();

param.put("businesstype","1006");

param.put("trade_no","123123123");

param.put("orderid","123456");

getmethodmethod=getmethod(url,param);

client.executemethod(method);

if(method.getstatuscode()!=200){

fail("未正确響應");

return;

}

stringresponse=method.getresponsebodyasstring();

system.err.println(response);

paystatusresponsepaystatus=newpaystatusresponse();

paystatus.setbusinesstype(1006);

paystatus.setorderid("");

paystatus.setresultstatus(1);

stringpaystatusjson=jsonutil.tojson(paystatus);

system.err.println(paystatusjson);

assertequals(paystatusjson,response);

*/

    json和對象之間轉換用jackson架構

  2、springmvc可以使用mockmvc

  staticimports:

mockmvcbuilders.*,mockmvcrequestbuilders.*,mockmvcresultmatchers.*

webapplicationcontextwac=...;

mockmvcmockmvc=webappcontextsetup(wac).configurewarrootdir("src/main/webapp",false).build()

mockmvc.perform(get("/form"))

.andexpect(status().isok())

.andexpect(content().mimetype("text/html"))

.andexpect(forwardedurl("/web-inf/layouts/main.jsp"));

最新内容請見作者的github頁:http://qaseven.github.io/