幸運的是,Mock Object可以用來模擬一些我們需要的類,這些對象被稱之為模仿對象,在單元測試中它們特別有價值。
Mock Object用于模仿真實對象的方法調用,從而使得測試不需要真正的依賴對象。Mock Object只為某個特定的測試用例的場景提供剛好滿足需要的最少功能。它們還可以模擬錯誤的條件,例如拋出指定的異常等。
目前,有許多可用的Mock類庫可供我們選擇。一些Mock庫提供了常見的模仿對象,例如:HttpServletRequest,而另一些Mock庫則提供了動態生成模仿對象的功能,本文將討論使用EasyMock動態生成模仿對象以便應用于單元測試。
到目前為止,EasyMock提供了1.2版本和2.0版本,2.0版本僅支持java SE 5.0,本例中,我們選擇EasyMock 1.2 for Java 1.3版本進行測試,可以從http://www.easymock.org下載合適的版本。
我們首先來看一個用戶驗證的LoginServlet類:
/**
* LoginServlet.java
* Author: Liao Xue Feng, www.crackj2ee.com
*/
package com.crackj2ee.test.mock;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
PRotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String passWord = request.getParameter("password");
// check username & password:
if("admin".equals(username) && "123456".equals(password)) {
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getNamedDispatcher("dispatcher");
dispatcher.forward(request, response);
}
else {
throw new RuntimeException("Login failed.");
}
}
}
這個Servlet實現簡單的用戶驗證的功能,若用戶名和口令匹配“admin”和“123456”,則請求被轉發到指定的dispatcher上,否則,直接拋出RuntimeException。
為了測試doPost()方法,我們需要模擬HttpServletRequest,ServletContext和RequestDispatcher對象,以便脫離J2EE容器來測試這個Servlet。
我們建立TestCase,名為LoginServletTest:
public class LoginServletTest extends TestCase {
}
我們首先測試當用戶名和口令驗證失敗的情形,演示如何使用EasyMock來模擬HttpServletRequest對象:
public void testLoginFailed() throws Exception {
MockControl mc = MockControl.createControl(HttpServletRequest.class);
HttpServletRequest request = (HttpServletRequest)mc.getMock();
// set Mock Object behavior:
request.getParameter("username");
mc.setReturnValue("admin", 1);
request.getParameter("password");
mc.setReturnValue("1234", 1);
// ok, all behaviors are set!
mc.replay();
// now start test:
LoginServlet servlet = new LoginServlet();
try {
servlet.doPost(request, null);
fail("Not caught exception!");
}
catch(RuntimeException re) {
assertEquals("Login failed.", re.getMessage());
}
// verify:
mc.verify();
}
仔細觀察測試代碼,使用EasyMock來創建一個Mock對象需要首先創建一個MockControl:
MockControl mc = MockControl.createControl(HttpServletRequest.class);
然后,即可獲得MockControl創建的Mock對象:
HttpServletRequest request = (HttpServletRequest)mc.getMock();
下一步,我們需要“錄制”Mock對象的預期行為。在LoginServlet中,先后調用了request.getParameter("username")和request.getParameter("password")兩個方法,因此,需要在MockControl中設置這兩次調用后的指定返回值。我們期望返回的值為“admin”和“1234”:
request.getParameter("username"); // 期望下面的測試將調用此方法,參數為"username"
mc.setReturnValue("admin", 1); // 期望返回值為"admin",僅調用1次
request.getParameter("password"); // 期望下面的測試將調用此方法,參數為" password"
mc.setReturnValue("1234", 1); // 期望返回值為"1234",僅調用1次
緊接著,調用mc.replay(),表示Mock對象“錄制”完畢,可以開始按照我們設定的方式運行,我們對LoginServlet進行測試,并預期會產生一個RuntimeException:
LoginServlet servlet = new LoginServlet();
try {
servlet.doPost(request, null);
fail("Not caught exception!");
}
catch(RuntimeException re) {
assertEquals("Login failed.", re.getMessage());
}
由于本次測試的目的是檢查當用戶名和口令驗證失敗后,LoginServlet是否會拋出RuntimeException,因此,response對象對測試沒有影響,我們不需要模擬它,僅僅傳入null即可。
最后,調用mc.verify()檢查Mock對象是否按照預期的方法調用正常運行了。
運行JUnit,測試通過!表示我們的Mock對象正確工作了!
下一步,我們來測試當用戶名和口令匹配時,LoginServlet應當把請求轉發給指定的RequestDispatcher。在這個測試用例中,我們除了需要HttpServletRequest Mock對象外,還需要模擬ServletContext和RequestDispatcher對象:
MockControl requestCtrl = MockControl.createControl(HttpServletRequest.class);
HttpServletRequest requestObj = (HttpServletRequest)requestCtrl.getMock();
MockControl contextCtrl = MockControl.createControl(ServletContext.class);
final ServletContext contextObj = (ServletContext)contextCtrl.getMock();
MockControl dispatcherCtrl = MockControl.createControl(RequestDispatcher.class);
RequestDispatcher dispatcherObj = (RequestDispatcher)dispatcherCtrl.getMock();
按照doPost()的語句順序,我們設定Mock對象指定的行為:
requestObj.getParameter("username");
requestCtrl.setReturnValue("admin", 1);
requestObj.getParameter("password");
requestCtrl.setReturnValue("123456", 1);
contextObj.getNamedDispatcher("dispatcher");
contextCtrl.setReturnValue(dispatcherObj, 1);
dispatcherObj.forward(requestObj, null);
dispatcherCtrl.setVoidCallable(1);
requestCtrl.replay();
contextCtrl.replay();
dispatcherCtrl.replay();
然后,測試doPost()方法,這里,為了讓getServletContext()方法返回我們創建的ServletContext Mock對象,我們定義一個匿名類并覆寫getServletContext()方法:
LoginServlet servlet = new LoginServlet() {
public ServletContext getServletContext() {
return contextObj;
}
};
servlet.doPost(requestObj, null);
最后,檢查所有Mock對象的狀態:
requestCtrl.verify();
contextCtrl.verify();
dispatcherCtrl.verify();
運行JUnit,測試通過!
倘若LoginServlet的代碼有誤,例如,將context.getNamedDispatcher("dispatcher")誤寫為 context.getNamedDispatcher("dispatcher2"),則測試失敗,JUnit報告:
junit.framework.AssertionFailedError:
UneXPected method call getNamedDispatcher("dispatcher2"):
getNamedDispatcher("dispatcher2"): expected: 0, actual: 1
getNamedDispatcher("dispatcher"): expected: 1, actual: 0
at ...
完整的LoginServletTest代碼如下:
/**
* LoginServletTest.java
* Author: Liao Xue Feng, www.crackj2ee.com
*/
package com.crackj2ee.test.mock;
import javax.servlet.*;
import javax.servlet.http.*;
import org.easymock.*;
import junit.framework.TestCase;
public class LoginServletTest extends TestCase {
public void testLoginFailed() throws Exception {
MockControl mc = MockControl.createControl(HttpServletRequest.class);
HttpServletRequest request = (HttpServletRequest)mc.getMock();
// set Mock Object behavior:
request.getParameter("username");
mc.setReturnValue("admin", 1);
request.getParameter("password");
mc.setReturnValue("1234", 1);
// ok, all behaviors are set!
mc.replay();
// now start test:
LoginServlet servlet = new LoginServlet();
try {
servlet.doPost(request, null);
fail("Not caught exception!");
}
catch(RuntimeException re) {
assertEquals("Login failed.", re.getMessage());
}
// verify:
mc.verify();
}
public void testLoginOK() throws Exception {
// create mock:
MockControl requestCtrl = MockControl.createControl(HttpServletRequest.class);
HttpServletRequest requestObj = (HttpServletRequest)requestCtrl.getMock();
MockControl contextCtrl = MockControl.createControl(ServletContext.class);
final ServletContext contextObj = (ServletContext)contextCtrl.getMock();
MockControl dispatcherCtrl = MockControl.createControl(RequestDispatcher.class);
RequestDispatcher dispatcherObj = (RequestDispatcher)dispatcherCtrl.getMock();
// set behavior:
requestObj.getParameter("username");
requestCtrl.setReturnValue("admin", 1);
requestObj.getParameter("password");
requestCtrl.setReturnValue("123456", 1);
contextObj.getNamedDispatcher("dispatcher");
contextCtrl.setReturnValue(dispatcherObj, 1);
dispatcherObj.forward(requestObj, null);
dispatcherCtrl.setVoidCallable(1);
// done!
requestCtrl.replay();
contextCtrl.replay();
dispatcherCtrl.replay();
// test:
LoginServlet servlet = new LoginServlet() {
public ServletContext getServletContext() {
return contextObj;
}
};
servlet.doPost(requestObj, null);
// verify:
requestCtrl.verify();
contextCtrl.verify();
dispatcherCtrl.verify();
}
}
總結:
雖然EasyMock可以用來模仿依賴對象,但是,它只能動態模仿接口,無法模仿具體類。這一限制正好要求我們遵循“針對接口編程”的原則:如果不針對接口,則測試難于進行。應當把單元測試看作是運行時代碼的最好運用,如果代碼在單元測試中難于應用,則它在真實環境中也將難于應用??傊瑒摻ūM可能容易測試的代碼就是創建高質量的代碼。
(出處:http://www.49028c.com)
新聞熱點
疑難解答