我的任務是爲MVC框架工作創建Unit Testing
。我試圖在谷歌搜索它,而不是隻是MVC,我看到的是爲Spring MVC。
我對JUnit測試的基本語法有一些想法
我是單元測試新手,所以我只知道那麼多。
那麼請你給我一個關於如何單元測試MVC的示例。我真的必須設置Required Dependencies with Maven
嗎?
MVC框架單元測試java
UPDATE:
/**
* Servlet implementation class LoginController
*/
public class LoginController extends HttpServlet {
/**
* Determines the version number for this serializable class.
*/
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginController() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
Logger log = Logger.getLogger(getClass());
HttpSession session = request.getSession();
String username = request.getParameter(SessionUtility.USERNAME);
String password = request.getParameter(SessionUtility.PASSWORD);
RequestDispatcher rd = null;
boolean withError = false;
if(request.getParameter(RegistrationController.UPDATE_PASSWORD_BUTTON) != null &&
request.getParameter(RegistrationController.UPDATE_PASSWORD_BUTTON).equalsIgnoreCase("updatepass"))
{
String userId = request.getParameter("userid");
String newPassword = request.getParameter("newpassword");
String oldPassword = request.getParameter("oldpassword");
UsersDAO userDAO = new UsersDAO();
if(userDAO.isUserPasswordMatch(userId, oldPassword))
{
userDAO.setNewPassword(newPassword, userId);
request.getSession().setAttribute(ProntoUtility.SUCCESS_MESSAGE, "Password successfully updated.");
}
else
{
request.setAttribute(ProntoUtility.ERROR_MESSAGE, "Old password did not match.");
}
rd = request.getRequestDispatcher(ProntoUtility.STATE_TABLE_DISPLAY);
try
{
rd.forward(request, response);
}
catch(ServletException e)
{
log.error("ServletException");
}
catch(IOException e)
{
log.error("IOException");
}
return;
}
else if(session.getAttribute(SessionUtility.SESSION_NAME) != null)
{
session.getAttribute(SessionUtility.SESSION_TYPE);
rd = request.getRequestDispatcher(ProntoUtility.STATE_TABLE_DISPLAY);
withError = true;
}
else if((username == null || password == null) && !withError)
{
rd = request.getRequestDispatcher(ProntoUtility.LOGIN_PAGE);
withError = true;
}
else if((username == "" || password == "") && !withError)
{
request.setAttribute(ProntoUtility.ERROR_MESSAGE, "Please fill-up the required fields.");
rd = request.getRequestDispatcher(ProntoUtility.LOGIN_PAGE);
withError = true;
}
else if(!withError)
{
String encryptedPassword = PasswordEncryption.encryptPassword(password);
UsersDAO usersDAO = new UsersDAO();
UsersModel login = usersDAO.getUsernamePassword(username, encryptedPassword);
if(login != null)
{
String usernameDB = login.getUsername();
String passwordDB = login.getPassword();
String teamId = login.getTeamId();
String userName = login.getUsername();
int userId = login.getUserId();
if(usernameDB.equals(username) && passwordDB.equals(encryptedPassword))
{
session.setAttribute(SessionUtility.USERNAME, userName);
session.setAttribute(SessionUtility.SESSION_TEAM, teamId);
session.setAttribute(SessionUtility.SESSION_ID, userId);
session.setAttribute(SessionUtility.SESSION_NAME, usernameDB);
session.setAttribute(SessionUtility.SESSION_TYPE, login.getType());
session.setAttribute(SessionUtility.SESSION_PROJECT, login.getProjectId());
session.setAttribute(SessionUtility.SESSION_PROJECT_NAME, login.getProjectName());
rd = request.getRequestDispatcher(ProntoUtility.STATE_TABLE_DISPLAY);
withError = true;
}
else if(!withError)
{
request.setAttribute(ProntoUtility.ERROR_MESSAGE, "Incorrect username/password.");
rd = request.getRequestDispatcher(ProntoUtility.LOGIN_PAGE);
withError = true;
}
}
else if(!withError)
{
request.setAttribute(ProntoUtility.ERROR_MESSAGE, "Incorrect username/password.");
rd = request.getRequestDispatcher(ProntoUtility.LOGIN_PAGE);
withError = true;
}
}
try
{
if(withError == true)
rd.forward(request, response);
}
catch(ServletException e)
{
log.debug("Unable to forward to requested dispatcher", e);
}
catch(IOException e)
{
log.debug("Null forward request", e);
}
return;
}
/**
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
@Override
public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
{
doGet(paramHttpServletRequest, paramHttpServletResponse);
}
}
我說我的工作項目的樣本控制器。
如果您使用Google,則可以使用「-spring」告知Google返回沒有「spring」的頁面。 – sjngm
@sjngm我試圖用「-spring」進行搜索,但似乎還沒有爲MVC框架進行單元測試的教程。我如何UnitTest控制器?我是否必須將它與模型和視圖相關聯,而我在進行測試? – newbie
這將是值得發佈一些簡化的代碼示例,顯示您正在測試的代碼 - 儘管MVC模式已經很好建立,並且(如果實現的很好)確實支持良好的單元測試,但不同的人對模式的解釋不同,因此沒有'一刀切'的解決方案來測試MVC的實現。 – robjohncox