The "Edit Contact" Function
The last remaining piece of functionality to write is the "edit contact" functionality. First, write the test. Copy the EditTest.java.v1 file from the example code archive
to src/test/com/abcinc/phonelist/test and name it EditTest.java. You will also need to make changes to build.xml, so copy build.xml.v5 from the example code and use it to replace your current build.xml file. The changes you made to build.xml between v4 and v5 were:
- Modified the test target to include test-edit as a dependency
- Added a test-edit target that executes the EditTest class
Run the tests again. You should get the following error:
[java] 1) testEditContact(com.abcinc.phonelist.test.EditTest)com.meterware.httpunit.HttpException:
Error on HTTP request: 400 Invalid path /edit was requested [http://localhost:8080/phonelist/edit.do?id=6]
Now implement the "edit contact" functionality so that the EditTest suite will pass. Copy the EditAction.java.v1 file from the example code to src/WEB-INF/classes/com/abcinc/phonelist. Name the file EditAction.java. You also need to add a section to struts-config.xml. You can simply copy struts-config.xml.v4 from the example code over your existing struts-config.xml file in src/WEB-INF. The one change in struts-config.xml between v3 and v4 is that you added an action definition for the "/edit" action.
After redeploying (shut down Tomcat, undeploy, deploy, start up Tomcat), run the tests again. Your Web application should now pass all of the tests.
Test Your Skills After Testing the App
Try the following challenges to flex your skills in test-driven development:
- Add code to the testSaveNewContact method in NewTest that verifies that the "Cancel" button works properly on the "new contact" action. Although you perform a check to ensure that "Cancel" works for the "edit contact" action, you make no such check for the "new contact" action. If it works properly, you should be able to enter information in the fields of the "new contact" form, then click on "Cancel" and have the list of contacts not increase by one.
- Add a test to EditTest that verifies whether the validation is working properly. You could try programmatically posting the "edit contact" form with a blank last name or a blank first name.
- Rewrite the application to interface with a relational database instead of the in-memory storage you devised in ContactDatabase. This should simply involve changing ContactDatabase.java. Your tests will be useful to ensure that none of the functionality has been broken in the port over to a relational database for the backend data store.