MVC framework unit testing java -


my task create unit testing mvc frame work. tried search on google instead of mvc, see spring mvc.

i have idea on basic syntax of junit testing
i'm new unit testing know much.

so please can give me sample on how unit test mvc. have setup required dependencies maven?

update:

/**  * servlet implementation class logincontroller  */ public class logincontroller extends httpservlet {     /**      * determines version number 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 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 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 forward 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 );     } } 

i added sample controller of project i'm working on.

how use junit make unit tests?

if now: "but want test mvc, not learn how unit tests work in theory" here reply:

unit testing means test single class or unit (in java it's class) without interference other classes. of course application has more 1 class , use each other. mock objects can give tested class objects, provide necessary things class needs, not objects of class tested class uses on runtime. mock objects , feel real objects. specify values shall return on method calls.

that way can test single class without other class interfering. problems can thereby detected (just 1 possible source). write 1 unit test case each class.

as per-class solution, unimportant framework use. frameworks support testing (are unit-tested, support dependency injection) whereas others not (e.g. lot of singletons).

how can test given class?

well write junit test case (good ide supports that) single class only. if class extends class not code easy writing test class object parent. don't have test parent's methods, own. ideally constructor has no side-effects. initializes object stable state not start other actions. way can unit test classes extend class within own codebase. parent classes used constructor (and maybe method calls within class), can concentrate testing methods of subclass.

as write unit test every of classes, parent class. if problems in parent class, related test case find out (if written well).

coming question: first there nothing testing mvc. mvc design pattern. second in unit tests don't test interaction of classes (look functional tests).

functional tests

functional tests test interaction of subset of components. 1 functional test test login in cms. 1 login there needed multitude of classes. in testing timeline, first run unit tests , functional tests (as these require working classes). whenever doesn't work, have rerun procedure after fixed problems.

summary

you asked sample test "an mvc". explained earlier there nothing testing mvc it's pattern. shown servlet has 1 gigantic method (no comment on how structured -- or not is) , 1 method calls it, isn't build test friendly. in addition method protected need make white-box test opposed black-box testing.

a functional test may interesting allow use multiple classes , test interaction. if class example xyz , changes settings of class b, can test interaction calling method of class , compare expected result actual setting of class b.

with given code alone working test sample not practical.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -