我的任务是为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