私の仕事はUnit Testing
、MVC フレームワークを作成することです。Google で検索しようとしましたが、MVC だけでなく、Spring MVC が表示されます。
私はJUnit Testingの基本的な構文についていくつかのアイデアを持っています
。ユニットテストは初めてなので、それだけしか知りません。
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 );
}
}
私が取り組んでいるプロジェクトのサンプルコントローラーを追加しました。