public void testReadServletOutputStream() throws IOException {
SampleServlet servlet = new SampleServlet();
servlet.doGet(request, response);
}
public void endReadServletOutputStream(WebResponse theResponse)
throws IOException {
String expected = "<html><head/><body>A GET request</body></html>";
String result = theResponse.getText();
assertEquals(expected, result);
}
public void beginPostMethod(WebRequest theRequest) {
theRequest.addParameter("param", "value", WebRequest.POST_METHOD);
}
public void testPostMethod() {
SampleServlet servlet = new SampleServlet();
assertEquals("POST", servlet.checkMethod(request));
assertEquals("value", request.getParameter("param"));
}
}
第一個方法testReadServletOutputStream,調用doGet,相當于在客戶端提交請求,然后在Servlet處理后會產生一個回饋,所以,在endReadServletOutputStream方法里,我們通過調用response的相應方法判斷回饋是否符合預期結果。
第二個方法testPostMethod,在這之前有一個beginPostMethod,在這個方法里我們以POST方式往request里增加一個表單數據param,值為”value”。下面在testPostMethod我們就要驗證表單數據是否以POST方式提交到了服務端的Servlet里,所以,我們看到了兩個assertEquals,分別進行了判斷。在這里我們要注意到beginPostMethod方法中的theRequest和testPostMethod中的request的區別,在前面我們已經提到過,beginPostMethod是在客戶端執行的,所以它方法內的所有操作事實上是模擬頁面操作的,比如上面的設置表單數據,而testPostMethod是服務端執行的,其中的request也是服務端的。
配置cactus.properties和web.xmlcactus.properties
cactus.contextURL這個屬性是必須的,它指定了web應用的訪問地址
例:cactus.contextURL = http://localhost:8080/test
文章來源于領測軟件測試網 http://www.kjueaiud.com/