Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build a Java Web Application Using HttpUnit and the Test-driven Methodology, Part II : Page 5

Part I of this article series demonstrated how to set up a test-driven environment and a phone list Web application. Part II completes the demonstration, showing you how to fill out the functionality of the application and perform tests for those new functions.


advertisement
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:
  1. Modified the test target to include test-edit as a dependency
  2. 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:

  1. 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.
  2. 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.
  3. 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.



Wellie Chao has been active in the business of technology for many years, has been involved with software and hardware since 1984, and has been writing Web-based software in a variety of languages and on different platforms since 1994. .
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap