On 2/5/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I generally create a mock for the IRequestCycle (using EasyMock), so havent
> run
> into
> this kind of problem.
>
> From what I can guess from the code, you try to initialize the Tapestry system
> and have it along during the unit tests.
>
> Are there any real benefits to this, instead of using mocks?
>
> But anyway, you could extend the RequestCycle class, override its
> getPage method and have Creator construct a suitable page instance,
> couldnt you?
This is what I had with Tapestry 3.0.3, and using a MockRequestCycle
turns out to be much easier in Tapestry 4.0. Here's what I used:
public class MockRequestCycle extends RequestCycle {
public IPage getPage(String name) {
// convert the first character to uppercase
char first = Character.toUpperCase(name.charAt(0));
name = first + name.substring(1);
// if it ends in an "s", replace "s" with "List"
if (name.endsWith("s")) {
name = name.substring(0, name.length() - 1) + "List";
}
name = MockRequestCycle.class.getPackage().getName() + "." + name;
try {
return (IPage) new Creator().newInstance(Class.forName(name));
} catch (Exception e) {
// Instantiate a BasePage and hope that works
try {
return (IPage) new Creator().newInstance(BasePage.class);
} catch (Exception e2) {
e.printStackTrace();
throw new RuntimeException("Unable to instantiate '" +
name + "'");
}
}
}
}
Matt
>
> From Matt Raible <[EMAIL PROTECTED]>:
>
> > I'm in the midst of upgrading Equinox (http://equinox.dev.java.net)
> > from Tapestry 3.3 to 4.0. With the 3.3 version, I had a
> > tapestry-mock.jar that I created from Tapestry's CVS, as well as an
> > AbstractInstantiator class that I found on Howard's site.
> >
> > For the most part, the new Creator class is sufficient for testing my
> > pages. However, I haven't found a way to add pages to Tapestry so
> > when I call cycle.getPage('name'), things don't fail.
> >
> > Here's my listener method:
> >
> > public void save(IRequestCycle cycle) {
> > if (log.isDebugEnabled()) {
> > log.debug("entered 'save' method");
> > }
> >
> > if (getValidationDelegate().getHasErrors()) {
> > return;
> > }
> >
> > getUserManager().saveUser(getUser());
> >
> > UserList nextPage = (UserList) cycle.getPage("users");
> > nextPage.setMessage(getMessages().format("user.saved",
> > getUser().getFullName()));
> > throw new PageRedirectException(nextPage);
> > }
> >
> > Here's my test - first of all, my UserFormTest.java extends
> > BasePageTestCase.java:
> >
> > BasePageTestCase.java:
> >
> > public class BasePageTestCase extends
> > AbstractDependencyInjectionSpringContextTests {
> > protected final Log log = LogFactory.getLog(getClass());
> >
> > protected String[] getConfigLocations() {
> > return new String[] {"/WEB-INF/applicationContext*.xml"};
> > }
> >
> > protected IPage getPage(Class clazz) {
> > return getPage(clazz, null);
> > }
> >
> > protected IPage getPage(Class clazz, Map properties) {
> > Creator creator = new Creator();
> > if (properties == null) {
> > properties = new HashMap();
> > }
> >
> > return (IPage) creator.newInstance(clazz, properties);
> > }
> > }
> >
> > public class UserFormTest extends BasePageTestCase {
> > private UserForm page;
> > private Long userId;
> > private UserManager userManager;
> >
> > public void setUserManager(UserManager userManager) {
> > this.userManager = userManager;
> > }
> >
> > protected void onSetUp() throws Exception {
> > Map map = new HashMap();
> > map.put("userManager", userManager);
> > page = (UserForm) getPage(UserForm.class, map);
> >
> > // create a new user
> > User user = new User();
> > user.setFirstName("Jack");
> > user.setLastName("Raible");
> >
> > // persist to database
> > userManager.saveUser(user);
> > userId = user.getId();
> > }
> >
> > protected void onTearDown() throws Exception {
> > page = null;
> > }
> >
> > public void testSave() throws Exception {
> > RequestCycle cycle = new RequestCycle();
> > cycle.setServiceParameters(new Object[] {userId});
> >
> > page.edit(cycle);
> > assertNotNull(page.getUser());
> > try {
> > page.save(cycle);
> > fail("Redirect didn't occur");
> > } catch (PageRedirectException pe) {
> > assertNotNull("redirect failed", pe);
> > }
> > }
> >
> > }
> >
> > There error I'm getting is below. I'm able to test just about
> > everything - it's just the loading of pages that's failing (so far).
> >
> > java.lang.NullPointerException
> > at
> > org.apache.tapestry.engine.RequestCycle.loadPage(RequestCycle.java:290)
> > at
> > org.apache.tapestry.engine.RequestCycle.getPage(RequestCycle.java:251)
> > at org.appfuse.web.UserForm.save(UserForm.java:70)
> > at org.appfuse.web.UserFormTest.testSave(UserFormTest.java:55)
> > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > at
> > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> > at
> >
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> > at java.lang.reflect.Method.invoke(Method.java:324)
> > at junit.framework.TestCase.runTest(TestCase.java:154)
> > at junit.framework.TestCase.runBare(TestCase.java:127)
> > at junit.framework.TestResult$1.protect(TestResult.java:106)
> > at junit.framework.TestResult.runProtected(TestResult.java:124)
> > at junit.framework.TestResult.run(TestResult.java:109)
> > at junit.framework.TestCase.run(TestCase.java:118)
> > at
> >
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
> > at
> >
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
> > at
> >
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
> >
> > Thanks,
> >
> > Matt
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
>
>
> --
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]