STRUTS

Page 75

Testing Struts Actions with StrutsTestCase

62

Tip: Refactor your test code as you would with any code. Create utility classes to make testing easier.

The code above only tests the “happy-go-lucky� route. What if the user already exists in the system? What if the user hits the Cancel button? With TDD, you test anything that could possibly break. Here is the test case to see if a user already exists: public void testDuplicateUserRegistrations(){ /* Create the first registration */ String email = "rhightower@arc-mind.com"; JDBCTestUtils.createUser(connection, email); /* Set the action path */ setRequestPathInfo("/userRegistration"); populateUserForm(email); /* Run the action through the ActionServlet */ actionPerform(); /* Make sure that the Exception Handler error message is present */ verifyActionErrors(new String[]{"userRegistration.sql.exception"}); /* Make sure the Exception object got put into request scope */ Exception e = (Exception) getRequest() .getAttribute(Globals.EXCEPTION_KEY); assertNotNull(e); }

Jakarta Struts Live


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.