实例简介HttpUnit结合JUnit自动测试Web应用
2010-05-28 16:01
465 查看
本文通过实例来讲解如何通过HttpUnit来对web应用进行测试,尤其是当下Ajax越来越流行的情况下,http request和response交互频繁,里面传输的内容也以Json或者XML为主,用HttpUnit结合JUnit来做测试可以带来很多好处,甚至是在web页面还不存在的情况下,通过模拟http请求,包括模拟上传文件,就可以用来测试服务端的servlet,action(有httprequest参数)等代码.
JAVA实例代码
HTTPStub :包装了HttpUnit提供的一些类,同时在初始化的时候做login验证,WebConversation会维护session的信息.
public class HTTPStub {
private WebConversation httpConversation;
private PostMethodWebRequest httpRequest;
public HTTPStub() {
httpConversation = new WebConversation();
String urlLogin = EnvConstant.SERVER_CTXT + EnvConstant.SERVER_LOGINURL;
GetMethodWebRequest getReq = new GetMethodWebRequest(urlLogin);
try {
httpConversation.getResponse(getReq);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void initHttpRequest(String url) {
httpRequest = new PostMethodWebRequest(EnvConstant.SERVER_CTXT + url, true);
}
public void setParameter(String name, String value) {
httpRequest.setParameter(name, value);
}
public void setFile(String Filename) {
InputStream inputStream = FileUtil.readFromdefaultClspath(Filename);
httpRequest.selectFile("dumyfile", "dumyfile.csv", inputStream, "text/plain");
}
public WebResponse getHttpResponse() {
try {
return httpConversation.getResponse(httpRequest);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String getHttpResponseContents() {
try {
WebResponse resp = httpConversation.getResponse(httpRequest);
StringBuffer strbf = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(resp.getInputStream()));
String str;
while ((str = in.readLine()) != null) {
strbf.append(str);
}
in.close();
return strbf.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
对inputstream处理的一个util类:
public class FileUtil {
public static InputStream readFromdefaultClspath(String fileName) {
InputStream stream = ClassLoader.getSystemResourceAsStream(fileName);
return stream;
}
public static String getContentsFromFile(String fileName) {
InputStream stream = readFromdefaultClspath(fileName);
StringBuffer strbf = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str;
while ((str = in.readLine()) != null) {
strbf.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return strbf.toString();
}
}
Junit测试类:
public class ActionCopyBillTest {
private HTTPStub httpStub;
@Before
public void setUp() throws Exception {
httpStub = new HTTPStub();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPerform() {
httpStub.initHttpRequest("FrontController?command=CopyBill");
httpStub.setParameter("bm_cb_dtCategory", "Copy Bill Request");
httpStub.setParameter("bm_cb_SRID", "SR0001");
httpStub.setParameter("bm_cb_ItemOpt", "2- Custom Itemisation");
httpStub.setParameter("bm_cb_BillLanCode", "ENG");
httpStub.setParameter("bm_cb_LegendPrs", "Copy Legend");
httpStub.setParameter("bm_cb_BillStruct", "Front Page Only");
httpStub.setParameter("bm_cb_ItemThd", "1");
httpStub.setParameter("bm_cb_BillMedia", "Paper Bill");
httpStub.setParameter("bm_cb_BillFormat", "Blue Bill");
httpStub.setFile("testdata/req/CopyBill_1.csv");
String respContents = httpStub.getHttpResponseContents();
String ritContents = FileUtil.getContentsFromFile("testdata/rep/CopyBill_1.rsp");
Assert.assertEquals(respContents, ritContents);
}
}
如果想对response进行验证,可以通过手工从html页面输入数据,提交请求,用工具(如eclipse带有的插件tcp/ip monitor)将response截取下来保存为文件,然后和junit测试的时候的response对比.
另外,返回的response也提供了一系列方法来操作其包含的内容:
1,如返回的是文本,可以通过resp.getText()获取,如果文本是json格式,可以再进一步构造成jsonobject来操作.
String respContents = resp.getText();
JSONObject json = new JSONObject(respContents);
System.out.println(json.getInt("total"));
JSONArray arr = json.getJSONArray("userdata");
System.out.println(arr.get(0));
2,如果返回的是XML(标准结构的html也是合法的XML),可以得到w3c 的document对象,resp.getDOM();
3,如果返回的是html页面,WebResponse提供了一组类似于Javascript操作html dom的方法.
resp.getElementWithID(id)
resp.getTables();
...
可以在http://httpunit.sourceforge.net/doc/cookbook.html找到更详细的信息.
JAVA实例代码
HTTPStub :包装了HttpUnit提供的一些类,同时在初始化的时候做login验证,WebConversation会维护session的信息.
public class HTTPStub {
private WebConversation httpConversation;
private PostMethodWebRequest httpRequest;
public HTTPStub() {
httpConversation = new WebConversation();
String urlLogin = EnvConstant.SERVER_CTXT + EnvConstant.SERVER_LOGINURL;
GetMethodWebRequest getReq = new GetMethodWebRequest(urlLogin);
try {
httpConversation.getResponse(getReq);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void initHttpRequest(String url) {
httpRequest = new PostMethodWebRequest(EnvConstant.SERVER_CTXT + url, true);
}
public void setParameter(String name, String value) {
httpRequest.setParameter(name, value);
}
public void setFile(String Filename) {
InputStream inputStream = FileUtil.readFromdefaultClspath(Filename);
httpRequest.selectFile("dumyfile", "dumyfile.csv", inputStream, "text/plain");
}
public WebResponse getHttpResponse() {
try {
return httpConversation.getResponse(httpRequest);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String getHttpResponseContents() {
try {
WebResponse resp = httpConversation.getResponse(httpRequest);
StringBuffer strbf = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(resp.getInputStream()));
String str;
while ((str = in.readLine()) != null) {
strbf.append(str);
}
in.close();
return strbf.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
对inputstream处理的一个util类:
public class FileUtil {
public static InputStream readFromdefaultClspath(String fileName) {
InputStream stream = ClassLoader.getSystemResourceAsStream(fileName);
return stream;
}
public static String getContentsFromFile(String fileName) {
InputStream stream = readFromdefaultClspath(fileName);
StringBuffer strbf = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str;
while ((str = in.readLine()) != null) {
strbf.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return strbf.toString();
}
}
Junit测试类:
public class ActionCopyBillTest {
private HTTPStub httpStub;
@Before
public void setUp() throws Exception {
httpStub = new HTTPStub();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPerform() {
httpStub.initHttpRequest("FrontController?command=CopyBill");
httpStub.setParameter("bm_cb_dtCategory", "Copy Bill Request");
httpStub.setParameter("bm_cb_SRID", "SR0001");
httpStub.setParameter("bm_cb_ItemOpt", "2- Custom Itemisation");
httpStub.setParameter("bm_cb_BillLanCode", "ENG");
httpStub.setParameter("bm_cb_LegendPrs", "Copy Legend");
httpStub.setParameter("bm_cb_BillStruct", "Front Page Only");
httpStub.setParameter("bm_cb_ItemThd", "1");
httpStub.setParameter("bm_cb_BillMedia", "Paper Bill");
httpStub.setParameter("bm_cb_BillFormat", "Blue Bill");
httpStub.setFile("testdata/req/CopyBill_1.csv");
String respContents = httpStub.getHttpResponseContents();
String ritContents = FileUtil.getContentsFromFile("testdata/rep/CopyBill_1.rsp");
Assert.assertEquals(respContents, ritContents);
}
}
如果想对response进行验证,可以通过手工从html页面输入数据,提交请求,用工具(如eclipse带有的插件tcp/ip monitor)将response截取下来保存为文件,然后和junit测试的时候的response对比.
另外,返回的response也提供了一系列方法来操作其包含的内容:
1,如返回的是文本,可以通过resp.getText()获取,如果文本是json格式,可以再进一步构造成jsonobject来操作.
String respContents = resp.getText();
JSONObject json = new JSONObject(respContents);
System.out.println(json.getInt("total"));
JSONArray arr = json.getJSONArray("userdata");
System.out.println(arr.get(0));
2,如果返回的是XML(标准结构的html也是合法的XML),可以得到w3c 的document对象,resp.getDOM();
3,如果返回的是html页面,WebResponse提供了一组类似于Javascript操作html dom的方法.
resp.getElementWithID(id)
resp.getTables();
...
可以在http://httpunit.sourceforge.net/doc/cookbook.html找到更详细的信息.
相关文章推荐
- 实例简介HttpUnit结合JUnit自动测试Web应用
- 实例简介HttpUnit结合JUnit自动测试Web应用
- http://sourceforge.net/projects/easymock/ ant结合junit进行软件自动测试
- ant结合junit进行软件自动测试
- ant结合junit进行软件自动测试(转)
- 软件测试技术JUnit和单元测试入门简介--JUnit实例
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- [图解教程]Eclipse不可不知的用法之二:自动生成接口和JUnit测试类
- Selenium入门之结合Junit进行自动化测试
- Spring回顾之五 —— 测试,JUnit与SpringTest的完美结合
- JUnit+Mockito结合测试Spring MVC Controller
- HttpUnit: 一种在 WebSphere Studio 中测试 Web 应用程序的改进方式
- UnitTest的另类应用 自动测试webservice wcf
- selenium webdriver实现自动截图与Junit导出report测试报告
- 自动化测试开发实际应用系列(三)HTTP JSON协议接口测试自动化实例
- 【转】https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
- idea 基于junit测试和自动生成测试类
- Junit测试实例1
- JUnit测试骨架简介
- http请求测试实例(采用fastjson解析)