2011-08-22 53 views
1

如何使用junit和jmock來測試MVC模型2應用程序?如何使用junit和jmock單元測試MVC模型2應用程序?

我有一個MVC模型2應用程序功能齊全,但沒有單元測試。我正在執行應用程序的單元測試。我想單元測試我的控制器servlet方法。控制器servlet調用dao類來執行CRUD操作。例如:用戶會看到一個登錄對話框,用戶輸入userid/pwd並點擊「提交」按鈕。該請求由控制器的processRequest()和validateLogin()方法處理,然後該方法通過DAO類查詢數據庫,如果登錄成功,最終返回包含數據的LoginBean。

我的問題是我該如何測試這種類型的代碼?

爲了更好地理解問題,下面提供了示例代碼。

---- ---- ControllerServlet.java

public class ControllerServlet extends HttpServlet { 
private static final String CONTENT_TYPE = "text/html; charset=windows-1252"; 

@Override 
public void init(ServletConfig config) throws ServletException { 
    super.init(config); 
} 

@Override 
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    doGet(request, response); 
} 

public void processRequest(HttpServletRequest request, HttpServletResponse response, String opcode) throws ServletException, IOException { 
    String url = "index.jsp"; //default url 
    HttpSession session = request.getSession(); 
    try { 
     if (opcode.equalsIgnoreCase("authenticate")) { 
      String firstName = GenericUtils.getParameter(request, "firstName"); 
      String lastName = GenericUtils.getParameter(request, "lastName"); 
      List userLst = validateLogin(firstName, lastName, request); 
      boolean loginValid = CollectionUtils.isNotEmpty(userLst); 
      request.setAttribute("loginValid", String.valueOf(loginValid)); //setting value in request attribute 

      //if login is valid set the LoginBean in session 
      if (loginValid) { 
       LoginBean loginBean = (LoginBean) userLst.get(0); 
       session.setAttribute("loginBean", loginBean); 
       getAllProducts(request); 
       url = "listProducts.jsp"; 
      } else { 
       url = "login.jsp"; 
      } 
     } 
} 
catch..... 
//some more code here 
} 

public List validateLogin(String firstName, String lastName, HttpServletRequest request) throws ServletException, IOException { 
    List userLst = new ArrayList(); 
    if (firstName.length() > 0 && lastName.length() > 0) { 
     userLst = MasterDao.checkLoginValid(firstName, lastName);//DAO call goes here 
    } 
    return userLst; 
} 

}

我怎麼能單元測試這種東西?我嘗試使用junit和jmock創建單元測試,但我不確定如何使用jmock傳遞請求參數,第二個問題是我使用JNDI在服務器啓動時創建數據庫連接,因此我的單元測試總是失敗DAO調用完成,因爲單元測試在編譯時被執行。我如何解決這兩個問題並單元測試這個功能?

回答

1

我會使用Spring網絡模擬框架 http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/mock/web/package-summary.html

import org.agitar.mock.servlet.MockHttpServletRequest; 
import org.agitar.mock.servlet.MockHttpServletResponse; 
import org.junit.Before; 
import org.springframework.mock.web.MockServletConfig; 


public class ControllerServletTest { 
    private ControllerServlet servlet; 
    private MockServletConfig config; 
    private MockHttpServletRequest request; 
    private MockHttpServletResponse response; 

    @Before 
    public void setUp() 
      throws Exception { 
     servlet = new ControllerServlet(); 
     config = new MockServletConfig(); 
     servlet.init(config); 

     request = new MockHttpServletRequest(); 
     response = new MockHttpServletResponse(); 
    } 

    @Test 
    public void authenticate() throws Exception { 
     request.putParameter("firstName", "Joe"); 
     request.putParameter("lastName", "Schmoe"); 

     // etc ... 

     servlet.processRequest(request, response, "authenticate"); 

     // etc ... 
    } 

} 
+0

感謝您的指針。我認爲這真的很有幫助。但是,您有什麼機會知道可以應用於當前場景的基於Web的應用程序的其他單元測試框架嗎? – user477402

+0

如果您覺得它有幫助,請給予答案投票和/或接受。 Selenium,JWebUnit,Canoo等是旨在測試Web應用程序的測試框架。有些可能需要使用運行容器進行測試。上述方法允許在正在運行的容器外進行測試。 –