|
From: Keith D. <ke...@in...> - 2005-03-21 23:35:28
|
Will,
Attached are sample action unit tests I just wrote for the Phonebook sample.
I'm still implementing MockRequestContext, but will commit soon. There
really isn't a sandbox-test area, so MockRequestContext will stay in the
sandbox/test src tree alongside "AbstractFlowExecutionTests" until both of
those are moved over for inclusion in spring-mock.jar.
Keith
public class TestGetPersonAction extends TestCase {
public void testGetPerson() throws Exception {
MockControl control =
MockControl.createControl(PhoneBook.class);
PhoneBook phoneBook = (PhoneBook)control.getMock();
phoneBook.getPerson(new Long(1));
control.setReturnValue(new Person(), 1);
control.replay();
GetPersonAction action = new GetPersonAction();
action.setPhoneBook(phoneBook);
MockRequestContext context = new MockRequestContext();
context.getFlowScope().setAttribute("id", new Long(1));
Event result = action.execute(context);
assertEquals("success", result.getId());
assertAttributePresent(context.getRequestScope(), "person");
control.verify();
}
public void testGetPersonDoesNotExist() throws Exception {
MockControl control =
MockControl.createControl(PhoneBook.class);
PhoneBook phoneBook = (PhoneBook)control.getMock();
phoneBook.getPerson(new Long(1));
control.setThrowable(new DataRetrievalFailureException("no such
person"), 1);
control.replay();
GetPersonAction action = new GetPersonAction();
action.setPhoneBook(phoneBook);
MockRequestContext context = new MockRequestContext();
context.getFlowScope().setAttribute("id", new Long(2));
Event result = action.execute(context);
assertEquals("error", result.getId());
assertAttributeNotPresent(context.getRequestScope(), "person");
control.verify();
}
}
_____
From: spr...@li...
[mailto:spr...@li...] On Behalf Of
Will Butler
Sent: Monday, March 21, 2005 5:34 PM
To: spr...@li...
Subject: [Springframework-developer] Spring Web Flow
All,
We have been using the Ervacon version of Spring Web Flow since version 0.7,
and we are now attempting to migrate to the new code in the Spring sandbox.
In the process, however, I noticed that the decoupling of actions from the
request/response has made them more difficult to test. Creating a
RequestContext requires an Event and a FlowExecutionStack which requires...
Anyway, I realize that we could create a Mock of the RequestContext, but
this also requires that child elements like Scope need to be mocked out as
well. What is the motivation behind the additional layers of abstraction and
why are they necessary? Does anyone best practices and/or examples for unit
testing actions in the new design?
Thanks,
Will Butler
|