Getting Started 21 - CONTROLLER unit test
Now suppose you want to design a form login web program, there is a form login page in the program, a loginaction that verifies the user name and the password, and a login success page, we intend to use the Spring MVC framework to implement. The most important thing for this form login program is to verify that the user name and the password are loginaction. The other two is just the page rendering, we want to make unit testing of this loginaction, and hope to use "test driver" to develop, To this end, we must first design the test case. Undoubtedly, our LoginAction must obtain form login information from the request, however, our TestCase is bound to be dependent on the servlet container, so unable to separate pure unit testing, but "container test" In order to be independent of the container for unit testing, we design a loginform, which is responsible for storeing the form login information in the request, and there is a HandleForm () method on the LoginAction, which is responsible for handling the LoginForm object and returns a ModelandView object. Don't consider how loginform collects login information from the request, purely tested the above design, we can make our loginaction to unpaged to the container to implement the LoginAction unit test, we use JUnit to test, let us first Write the following test case:
TestspringAppController.java
Package onlyfun.caterpillar.test;
Import junit.framework.testcase;
Import org.springframework.context.ApplicationContext;
Import org.springframework.context.support.filesystemxmlapplicationContext;
Import org.springframework.Web.Servlet.ModelandView;
Import onlyfun.caterpillar.loginaction;
Import onlyfun.caterpillar.loginform;
Public Class TestspringAppController Extends Testcase {
Private loginaction loginaction;
PRIVATE LOGINFORM FORM;
Public static void main (String [] args) {
JUnit.textui.teStrunner.run (TestspringAppController.class);
}
Protected void setup () throws exception {
Super.setup ();
ApplicationContext ApplicationContext = New FileSystemXmlapplicationContext ("/ Web-Inf / Hello-Servlet.xml");
ApplicationContext.getBean ("LoginAction");
Form = new loginform ();
}
Public void testlogincorRect () {
Form.SetuserName ("Caterpillar");
Form.SetPassword ("123456"); ModelandView ModelandView = loginaction.handleform (Form);
Assertequals (LoginAction.getsuccessView (), ModelandView.getViewName ());
}
Public void testloginincorRect () {
Form.SetuserName ("SomeBody");
Form.SetPassword ("123456");
ModelandView ModelandView = LoginAction.HandleForm (Form);
Assertequals (LoginAction.getformView (), ModelandView.getViewName ());
Form.SetuserName ("Caterpillar");
Form.SetPassword ("111111");
ModelandView = loginaction.handleform (Form);
Assertequals (LoginAction.getformView (), ModelandView.getViewName ());
}
}
The loginaction is set to getFormView () and getSuccessView (), indicating that the login failed and successful page, and we use the VIEWNAME asking in the modelandView in HandleForm (), and use the getViewName () when logging in to fail, should be LoginAction's GetFormView () has the same name, while the name of GetViewName () when the login is successful, should be the same as the name of the LoginAction's getSuccessView (). Note that this test case does not use objects related to the servlet API, which is a pure unit test. According to the test case, we can design our loginform and loginaction, first Loginform as follows, just pure JavaBean:
Loginform.java
Package online.
Public class loginform {
PRIVATE STRING UserName;
PRIVATE STRING Password;
Public void setusername (String username) {
THIS.USERNAME = UserName;
}
Public void setpassword (string password) {
this.password = password;
}
Public string getUsername () {
Return UserName;
}
Public string getpassword () {
Return Password;
}
}
Loginaction.java
Package online.
Import java.io.ioException;
Import javax.servlet. *;
Import javax.servlet.http. *;
Import Org.SpringFramework.Web.Servlet.mvc.controller;
Import org.springframework.Web.Servlet.ModelandView;
Import org.springframework.Web.Bind.RequestUtils;
Public Class Loginaction Implements Controller {Private Strument SuccessView;
PRIVATE STRING FORMVIEW;
Public ModilandView HandleRequest (httpservletRequest Req, HttpservletResponse Res) throws servletexception, ioException {
String UserName = RequestUtils.getRequiredStringParameter (Req, "UserName");
String password = RequestUtils.getRequiredStringParameter (Req, "password");
Loginform form = new loginform ();
Form.setUsername (username);
Form.SetUsername (Password);
Return HandleForm (Form);
}
Public ModilandView Handleform (Object FormObject) {
Loginform form = (loginform) FormObject;
IF ("Caterpillar" .Equals (form.getusername ()) &&
"123456" .Equals (form.getpassword ()))) {
Return New ModelandView (this.getsuccessView (), "user", form.getusername ());
}
Else {
Return New ModelandView (this.GetformView ());
}
}
Public string getformview () {
Return Formview;
}
Public void setFormview (string formview) {
THIS.FORMVIEW = Formview;
}
Public string getsuccessView () {
Return SuccessView;
}
Public void setsuccessView (string successview) {
THIS.SUCCESSVIEW = SUCCESSVIEW;
}
}
As you can see in LoginAction, the parameters in the request are collected through the loginform, and then handed over to our customs, we can implement the decoupled coupling with the Servlet API to make the unit test change. Of course, don't forget that we have to write bean definitions:
XML Version = "1.0" encoding = "UTF-8"?>
proty>
Property>
bean>
beans>
Next you can run the test case to test, after the test is correct, we can continue to configure other components in the Spring MVC framework.