我的任务是Unit Testing
为 MVC 框架工作。我试图在谷歌上搜索它,但我看到的不仅仅是 MVC,而是 Spring MVC。
我对 JUnit 测试的基本语法有一些想法
我是单元测试的新手,所以我只知道这么多。
所以请你给我一个关于如何对 MVC 进行单元测试的示例。我真的需要设置Required Dependencies with Maven
吗?
更新:
/**
* 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 );
}
}
我添加了我正在处理的项目的示例控制器。