Hi all,

I've been evaluating Wicket and I'm excited about using it. As part of my
evaluation I have been investigating how easy it would be to test my pages,
especially those containing forms.  

So my testing evaluation set out to answer these questions:

1. Can I use Guice injection in a way that I can test my forms without the
need for a test database.
2. Can I test that my forms initialize properly --  test that the form
renders with correct values given a model with know values.
3. Can I test that my model is updated properly when I submit the form and
that my (mock) dao is called to perform the save.
4. Can I test that the response page is what I expect when when I first
render and when i submit my form


Happily I can report that I was able to do the first three items by using
the testing examples provided on the wiki and a little ingenuity. 

I figured out how to extend the WicketTester to do Guice injection (pretty
simple) with a little playing around. Then I was able to mock my DAO (using
JMock) which then gets injected by Guice via my GuiceWicketTester. This is
great: my tests can run without a backend DB.

I was getting pretty excited, but now I'm kind of stuck on #4 because I have
a case in my testing that, when an existing object is modified on my form,
then submitted, my response page is different than my request page. My test
would actually pass, but when the response page renders IT fails.  

So, call me crazy, but it occurs to me that it would be nice if I could
"unit test" just my page, verifying that when the code in my page finishes
executing everything is what I expect. I don't really care if the response
page renders properly (I'll test that in a unit test for that page), I just
want to know that the logic in my page in sending off to the new page.

Does anyone have an idea for me? I've been looking at WickeTester to see if
I could override it somehow to prevent it from rendering the response page,
but I'm a still new to Wicket and its "RequestCycle" processing and so I'm
fumbling around a bit.

Thanks for any help,


matt clevenger


See code below:



import ...

public class PartDetailPageTest extends TestCase {
       
        ...

        private class MockModule<T> extends AbstractModule{
                private T mock;
                private Class<T> type;
                
                public MockModule(Class<T> type, Mockery mockery){
                        this.mock = mockery.mock(type);
                        this.type = type;
                }
                
                public T getmock(){
                        return mock;
                }
                
                public void configure(){
                        bind(type).toInstance(mock);
                }
        }

        private class GuiceWicketTester extends WicketTester{
                public GuiceWicketTester(Module[] modules){
                        super();
                        GuiceComponentInjector gci = new 
GuiceComponentInjector(getApplication(),
modules);
                        getApplication().addComponentInstantiationListener(gci);
                }                       
        }

        @Before
        public void setUp(){
                mockery = new Mockery();
                module = new MockModule<PartDAO>(PartDAO.class, mockery);
                tester = new GuiceWicketTester(new Module[]{module});
        }


        @Test
        public void testSubmitExistingPart(){
                // setup for modifying a part (part has an id -- found by dao)
                setUp();
                final Part part = new MockPart(10);
                final PartDAO mockDAO = module.getmock();
                mockery.checking(new Expectations(){{
                        one(mockDAO).findPart(part.getId()); 
will(returnValue(part));
                        one(mockDAO).save(with(same(part)));
                }});
                PageParameters params = new PageParameters("id="+ part.getId());
                Page page = new PartDetailPage(params); 
                tester.startPage(page);
                
                // simulate entry of part data
                FormTester formTester = tester.newFormTester("inputForm");
                formTester.setValue("barcode", "barcode123");
                formTester.setValue("name", "nameXYZ");
                formTester.setValue("description", "descriptionXYZ");
                formTester.submit();
                
// >> I never get to the check below

                // verify move to list page
                tester.assertRenderedPage(PartListPage.class);
        }

}


Here is part of interest in the class under test:

...

        private boolean isNewPart() {
                Integer id = getPartModel().getId();
                return ((id == null) || (id == 0));
                }
        
        private Part getPartModel(){
                Part part = (Part) getModel().getObject();
                return part;
        }

        public void onSubmit()
        {
                boolean isNew = isNewPart();
                partDao.save(getPartModel());
                if (isNew){
                        // reset model so part data will be empty
                        setModel(buildModel());
                }
                else{

// This is whta I would like to test: that the response page is set properly
// NOT that the response page is "rendered" properly

                        setResponsePage(PartListPage.class);
                }
        }
...


Here is the stack trace:

org.apache.wicket.WicketRuntimeException: The Hibernate session factory has
not been initialized. This is normally done in DataApplication.init().
        at
net.databinder.DataStaticService.getHibernateSessionFactory(DataStaticService.java:43)
        at
net.databinder.DataStaticService.dataSessionRequested(DataStaticService.java:66)
        at
net.databinder.DataStaticService.getHibernateSession(DataStaticService.java:52)
        at
net.databinder.models.HibernateListModel.load(HibernateListModel.java:103)
        at
org.apache.wicket.model.LoadableDetachableModel.getObject(LoadableDetachableModel.java:116)
        at org.apache.wicket.Component.getModelObject(Component.java:1279)
        at
org.apache.wicket.markup.html.list.ListView.getViewSize(ListView.java:217)
        at
org.apache.wicket.markup.html.list.ListView.onPopulate(ListView.java:524)
        at
org.apache.wicket.markup.repeater.AbstractRepeater.onBeforeRender(AbstractRepeater.java:126)
        at org.apache.wicket.Component.internalBeforeRender(Component.java:803)
        at org.apache.wicket.Component.beforeRender(Component.java:835)
        at
org.apache.wicket.MarkupContainer.onBeforeRenderChildren(MarkupContainer.java:1488)
        at org.apache.wicket.Component.onBeforeRender(Component.java:3254)
        at org.apache.wicket.Component.internalBeforeRender(Component.java:803)
        at org.apache.wicket.Component.beforeRender(Component.java:835)
        at
org.apache.wicket.MarkupContainer.onBeforeRenderChildren(MarkupContainer.java:1488)
        at org.apache.wicket.Component.onBeforeRender(Component.java:3254)
        at org.apache.wicket.Component.internalBeforeRender(Component.java:803)
        at org.apache.wicket.Component.beforeRender(Component.java:835)
        at org.apache.wicket.Component.prepareForRender(Component.java:1853)
        at org.apache.wicket.Page.renderPage(Page.java:842)
        at
org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.respond(BookmarkablePageRequestTarget.java:225)
        at
org.apache.wicket.request.AbstractRequestCycleProcessor.respond(AbstractRequestCycleProcessor.java:103)
        at
org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1097)
        at org.apache.wicket.RequestCycle.step(RequestCycle.java:1166)
        at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1245)
        at org.apache.wicket.RequestCycle.request(RequestCycle.java:489)
        at
org.apache.wicket.protocol.http.MockWebApplication.processRequestCycle(MockWebApplication.java:371)
        at org.apache.wicket.util.tester.FormTester.submit(FormTester.java:563)
        at
com.idahotech.bioreagents.part.view.test.PartDetailPageTest.testSubmitExistingPart(PartDetailPageTest.java:131)
        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:585)
        at junit.framework.TestCase.runTest(TestCase.java:168)
        at junit.framework.TestCase.runBare(TestCase.java:134)
        at junit.framework.TestResult$1.protect(TestResult.java:110)
        at junit.framework.TestResult.runProtected(TestResult.java:128)
        at junit.framework.TestResult.run(TestResult.java:113)
        at junit.framework.TestCase.run(TestCase.java:124)
        at junit.framework.TestSuite.runTest(TestSuite.java:232)
        at junit.framework.TestSuite.run(TestSuite.java:227)
        at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
        at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


(Note: Yeah I know "Hibernate session factory..." yada yada. 

I haven't yet abstracted this from the List page.  However, this is exactly
the issue. 

I don't want to actually "render" this new page (look further down and you
will see: at org.apache.wicket.Page.renderPage(Page.java:842). 

I just want to verify that I am going there)





-- 
View this message in context: 
http://www.nabble.com/Using-the-WicketTester-WITHOUT-rendering-a-response-on-submit-tf4801394.html#a13737447
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to