At line 16 changed 1 line. |
* [4] Display the JSP in a browser and run the ActionTest |
* [4] Display the JSP in a browser and run the ControllerTest |
At line 47 changed 1 line. |
At this point, you won't be able to view the JSP in your browser because the <html:form> tag in personForm.jsp has ''action="editPerson"'' - and this url-mapping doesn't exist (yet) in action-servlet.xml. You can try it yourself (cd ../.. first) by setting up AppFuse on Tomcat using __ant setup-db setup-tomcat deploy__. |
At this point, you won't be able to view the JSP in your browser because the validation code (at the bottom of the page) requires that the JSP is invoked from the DispatchServlet. Therefore, we need to create a Controller for this JSP, and we should practice TDD and write our Test before we write our Controller. |
At line 49 changed 5 lines. |
Then, start Tomcat and then go to [http://localhost:8080/appfuse/personForm.jsp]. This will result in the following error: |
{{{ |
javax.servlet.jsp.JspException: Cannot retrieve mapping for action /savePerson |
}}} |
Therefore, we need to create an Action for this JSP, and we should probably create a Test before we write our Action. |
!!Create a new ControllerTest to test our Controller [#3] |
To create a JUnit Test for our Controller, start by creating a PersonControllerTest.java file in the test/web/**/action directory. |
At line 55 changed 2 lines. |
!!Create a new ActionTest to test our Action [#3] |
To create a StrutsTestCase Test for our Action, start by creating a PersonActionTest.java file in the test/web/**/action directory. |
;:%%(color: blue)''As usual, copy → save as an existing ControllerTest (i.e. UserControllerTest). Replace [[Uu]ser with [[P]erson.''%% |
At line 58 removed 6 lines. |
;:%%(color: blue)''As usual, copy → save as an existing ActionTest (i.e. UserActionTest). Replace [[Uu]ser with [[P]erson. You might want to make sure [Cactus|http://jakarta.apache.org/cactus] (StrutsTestCase is an extension of Cactus] tests are running before you copy an existing one. Run __ant test-cactus -Dtestcase=UserAction__ to verify the UserAction works. Stop Tomcat before you do this.''%% |
|
If you did copy UserActionTest, make sure and change ''UserFormEx'' to ''PersonForm''. The reason for ''UserFormEx'' is to support indexed properties and non-struts validation. Since the UserForm is generated, it's not very feasible to do it in the User.java object. |
|
When we do create an Action (in [step 4|4]), we're only going to create an __execute__ method, rather than all the different CRUD methods. So let's just test that method to start. |
|
At line 68 changed 4 lines. |
public class PersonActionTest extends BaseStrutsTestCase { |
|
public PersonActionTest(String name) { |
super(name); |
public class PersonControllerTest extends BaseControllerTestCase { |
private static Log log = LogFactory.getLog({personFormControllerTest.class); |
private PersonFormController c; |
private MockHttpServletRequest request; |
private ModelAndView mv; |
|
protected void setUp() { |
// needed to initialize a user |
super.setUp(); |
c = (UserFormController) ctx.getBean("personFormController"); |
At line 74 changed 6 lines. |
public void testExecute() { |
// test execute method |
setRequestPathInfo("/editPerson"); |
addRequestParameter("id", "1"); |
actionPerform(); |
verifyNoActionErrors(); |
protected void tearDown() { |
c = null; |
At line 73 added 10 lines. |
|
public void testEdit() throws Exception { |
log.debug("testing edit..."); |
request = newGet("/editUser.html"); |
request.addParameter("username", "tomcat"); |
|
mv = c.handleRequest(request, new MockHttpServletResponse()); |
|
assertEquals("userForm", mv.getViewName()); |
} |
At line 84 changed 1 line. |
Everything should compile at this point (ant compile-web) since we're not referring to the PersonAction directly in our test. However, if you try to run __ant test-cactus -Dtestcase=PersonAction__, it won't work (make sure Tomcat is ''not'' running if you decide to try this). |
Nothing will compile at this point (ant compile) since we need to create the PersonFormController that we're referring to in this test. |
At line 86 changed 1 line. |
!!Create a new Action [#4] |
!!Create a new Controller [#4] |
At line 88 changed 1 line. |
Now we have to create an Action (a.k.a. the Controller) to talk to our Manager and retrieve/save our data. In src/web/**/action, create a PersonAction.java file with the following contents: |
Now we have to create a Controller to talk to our Manager and retrieve/save our data. In src/web/**/action, create a PersonFormController.java file with the following contents: |