Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The "Delete Contacts" Function
You are ready to move on to building the tests for the remainder of the functionality: delete and edit. The delete functionality will be nice. Right now the testSaveNewContact test case of the NewTest test suite is messy. It creates a new contact, but does not delete the contact afterwards. The edit functionality should be easy to test because it will look very similar to the tests for the new functionality. Implement the delete functionality first by performing the following steps:
  1. Copy DeleteTest.java.v1 from the example code archive to the src/test/com/abcinc/phonelist/test directory and name the file DeleteTest.java.
  2. Copy build.xml.v4 over your existing build.xml. The new version of build.xml has the following changes: (1) the addition of a new target named test-delete; (2) the addition of test-delete to the test target as a dependency; (3) the addition of the Web application classes to the classpath for the tests so that the tests can pick up classes like ContactBean; (4) the addition of compile-classes to the compile-tests target as a dependency in order for the tests to make use of the compiled classes from the Web application; and (5) the change of the compile-tests target to add the Web application classes to the classpath for compilation of the tests.
  3. Copy ContactBean.java.v2 from the example code to the src/WEB-INF/classes/ com/abcinc/phonelist directory and name the file ContactBean.java, in place of your existing copy of ContactBean.java. You added a new "equals" method that allows you to compare two ContactBean instances for equality of all fields, which is used in DeleteTest.java.

Type ant test to verify that although the NewTest and ShowListTest test suites pass, the DeleteTest test suite fails one test. The check for the presence of the delete button passes, but the check to verify that the delete action works does not pass. You should get an error like the following:

[java] There was 1 error: [java] 1) testDeleteContact(com.abcinc.phonelist.test.DeleteTest)com.meterware.httpunit.HttpInternalErrorException:
Error on HTTP request: 500 Internal Error [http://localhost:8080/phonelist/delete.do]

If your test suite does not fail, it usually means you made an error in coding the test. Take the following step to implement the delete functionality so that your test will pass:

  1. Copy DeleteAction.java.v1 from the example code archive to src/WEB-INF/classes/com/abcinc/phonelist and name the file DeleteAction.java.

Ordinarily, you need to add a section to struts-config.xml for every action, but you already added a section for the "/delete" action. You added it in Part I in order to show the list of contacts with a form that allowed for selecting items for deletion. You may recall that Struts requires the definition of actions used in forms, so even though you had not yet implemented the "/delete" action, you still had to have the definition in struts-config.xml in order to display the form in the showList.jsp file.

Try the tests again. Make sure you shut down Tomcat, undeploy the application, deploy the application, and then start up Tomcat. The DeleteTest test suite should now pass all tests. A good thing to do now is update the NewTest test suite so that it cleans up after itself. Now that you have the delete function working properly, you can delete the new contact you created in NewTest to test the ability to create and save new contacts.

Replace your version of NewTest.java in src/test/com/abcinc/phonelist/test with NewTest.java.v3 from the example code. Make sure you keep the NewTest.java filename. If you examine the file, you will see that the testSaveNewContact method contains extra code to determine the ID value of the newly created contact and then submit the form on the showList page with the correct checkbox selected to the delete action.

Run ant clean to be sure that the build directory is clean, then run ant test again to verify that the NewTest test suite works properly and that it no longer leaves a new contact in the database every time it runs. You can point your browser at http://localhost:8080/phonelist/showList.do to verify that the list of contacts does not change after NewTest runs. Replace localhost with whatever machine Tomcat is running on.

Thanks for your registration, follow us on our social networks to keep up-to-date