Java Reference
In-Depth Information
assertEquals(1,
bean.getContext().getValidationErrors().size());
assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE,
trip.getDestination());
}
Notice that to invoke an event handler, we just need to add its name as a
parameter. After calling
execute
( ) on
MockRoundtrip
, we can retrieve the
action bean and verify whatever we need to—in this case, the number
of validation errors.
MockRoundtrip
also returns the destination, which
is the source page in this case because a validation error occurred.
Testing a valid contact form (that is, with the email field filled in) is
similar. In this case, we test for the destination to be back to the contact
list. While we're at it, we'll also verify that type conversion for the phone
number is working properly:
@Test
public void
testSaveValid()
throws
Exception {
MockRoundtrip trip =
new
MockRoundtrip(mockServletContext,
ContactFormActionBean.
class
, mockSession);
trip.setParameter("contact.email", "test@test.com");
trip.setParameter("contact.phoneNumber", "654-456-4567");
trip.execute("save");
ContactFormActionBean bean =
trip.getActionBean(ContactFormActionBean.
class
);
assertEquals(0,
bean.getContext().getValidationErrors().size());
PhoneNumber pn = bean.getContact().getPhoneNumber();
assertEquals("654", pn.getAreaCode());
assertEquals("456", pn.getPrefix());
assertEquals("4567", pn.getSuffix());
assertTrue(
trip.getDestination().startsWith("/ContactList.action"));
}
We're checking the destination URL; conveniently,
getDestination
( ) re-
turns the URL regardless of it being a forward or a redirect. We can also
use
getForwardUrl
( ) or
getRedirectUrl
( ) to not only test the destination but
also specifically verify which type of response was returned.
Finally, we can also use URLs instead of action beans to test requests
with
MockRoundtrip
. Simply specify the URL in the constructor, as in
new MockRoundtrip(mockServletContext, "/ContactForm.action")
.
Search WWH ::
Custom Search