Re: Using the WicketTester WITHOUT rendering a response on submit
Timo, I am trying to achieve something similar. How did you manage to assign the TestingWebApplication to your WicketTester?? Thanks! mclev wrote: mclev wrote: 4. Can I test that the response page is what I expect when when I first render and when i submit my form 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. So I figured out a way to do this by extending the WebRequestCycleProcessor.respond methods. : public class TestingWebRequestCycleProcessor extends WebRequestCycleProcessor { private boolean respondingSuspended = false; public TestingWebRequestCycleProcessor(){ super(); } public boolean isRespondingSuspended() { return respondingSuspended; } public void setRespondingSuspended(boolean respondingSuspended) { this.respondingSuspended = respondingSuspended; } @Override public void respond(RequestCycle requestCycle) { if (isRespondingSuspended()){ return; } super.respond(requestCycle); } @Override public void respond(RuntimeException e, RequestCycle requestCycle) { if (isRespondingSuspended()){ return; } super.respond(e, requestCycle); } } then overriding the WicketTester.DummyApplication: public class TestingWebApplication extends WicketTester.DummyWebApplication{ private TestingWebRequestCycleProcessor requestProcessor; @Override protected IRequestCycleProcessor newRequestCycleProcessor() { requestProcessor = new TestingWebRequestCycleProcessor(); return requestProcessor; } public void suspendResponding(){ requestProcessor.setRespondingSuspended(true); } public void unsuspendResponding(){ requestProcessor.setRespondingSuspended(false); } } now my test looks like this: @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); String barcode = 123; String name = ABC; String description = XYZ; enterFormData(formTester, barcode, name, description); testApp.suspendResponding(); // so response won't be rendered formTester.submit(); // verify values set properly in model; assertModelUpdated(part, barcode, name, description); // verify that response page is correct // this now works -- even though the page isn't really renderd tester.assertRenderedPage(PartListPage.class); } I'm still new to wicket so if anybody thinks this is this is crazy please let me know. -- View this message in context: http://www.nabble.com/Using-the-WicketTester-WITHOUT-rendering-a-response-on-submit-tp13737447p17407457.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using the WicketTester WITHOUT rendering a response on submit
On Thu, 22 May 2008, nitinkc wrote: I am trying to achieve something similar. How did you manage to assign the TestingWebApplication to your WicketTester?? Thanks! WicketTester wicket = new WicketTester(new TestingWebApplication()); wicket.startComponent/Panel/Page(... Best wishes, Timo -- Timo Rantalaiho Reaktor Innovations OyURL: http://www.ri.fi/ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using the WicketTester WITHOUT rendering a response on submit
On Thu, 15 Nov 2007, mclev wrote: I don't know how to do this when my fields are private: With reflection. Just remember to call Field.setAccessible(true). private PartDAO partDao; By the way, according to naming conventions of Sun it should be PartDao :) Timo -- I would like to know what you think of my solution -- again see http://www.nabble.com/forum/ViewPost.jtp?post=13757593framed=y my second post , where I overrode the RequestCycleProcessor to allow a test to suspend response processing. It's intreresting and quite small and elegant when compared to some hoops I've hopped through when testing Wicket apps :) To me it seems like a useful addition to the Wicket testing toolbox, maybe the core developers would know if it is in danger of breaking with updates. Best wishes, Timo -- Timo Rantalaiho Reaktor Innovations OyURL: http://www.ri.fi/ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using the WicketTester WITHOUT rendering a response on submit
mclev wrote: 4. Can I test that the response page is what I expect when when I first render and when i submit my form 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. So I figured out a way to do this by extending the WebRequestCycleProcessor.respond methods. : public class TestingWebRequestCycleProcessor extends WebRequestCycleProcessor { private boolean respondingSuspended = false; public TestingWebRequestCycleProcessor(){ super(); } public boolean isRespondingSuspended() { return respondingSuspended; } public void setRespondingSuspended(boolean respondingSuspended) { this.respondingSuspended = respondingSuspended; } @Override public void respond(RequestCycle requestCycle) { if (isRespondingSuspended()){ return; } super.respond(requestCycle); } @Override public void respond(RuntimeException e, RequestCycle requestCycle) { if (isRespondingSuspended()){ return; } super.respond(e, requestCycle); } } then overriding the WicketTester.DummyApplication: public class TestingWebApplication extends WicketTester.DummyWebApplication{ private TestingWebRequestCycleProcessor requestProcessor; @Override protected IRequestCycleProcessor newRequestCycleProcessor() { requestProcessor = new TestingWebRequestCycleProcessor(); return requestProcessor; } public void suspendResponding(){ requestProcessor.setRespondingSuspended(true); } public void unsuspendResponding(){ requestProcessor.setRespondingSuspended(false); } } now my test looks like this: @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); String barcode = 123; String name = ABC; String description = XYZ; enterFormData(formTester, barcode, name, description); testApp.suspendResponding(); // so response won't be rendered formTester.submit(); // verify values set properly in model; assertModelUpdated(part, barcode, name, description); // verify that response page is correct // this now works -- even though the page isn't really renderd tester.assertRenderedPage(PartListPage.class); } I'm still new to wicket so if anybody thinks this is this is crazy please let me know. -- View this message in context: http://www.nabble.com/Using-the-WicketTester-WITHOUT-rendering-a-response-on-submit-tf4801394.html#a13757593 Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using the WicketTester WITHOUT rendering a response on submit
On Tue, 13 Nov 2007, mclev wrote: 1. Can I use Guice injection in a way that I can test my forms without the need for a test database. In the little experimentation I've done, I have found that the most straightforward way with single components without contained guice-depndent components is to set the dependencies directly to the fields, without going via Guice. In more complex scenarios where you don't have control over the creation of all guice-dependent components you can easily add a custom GuiceComponentInstantiationListener to your Application in the test case after creating the Application but before creating the component under test. 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. This is unconventional as far as I know. Normally you would not extend WicketTester but maybe provide a testing subclass of your APplication to it and just make WicketTester use that (in 1.3, I think that in 1.2 this was more limited). Even that is not strictly needed as you can always add custom IComponentInstantiationListeners to any APplication, including DummyWebApplication that WicketTester uses by default. 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. Rendering the page causes some overhead (WicketTester tests seem to be a bit slower that orthodox unit tests, and XML parsing has shown up in some profiling we did a while back) but for me the added benefit of simplicity and automatic checking of matching component hierarchy in Java and HTML has outweighed the cost. Still your approach is academically very interesting :) BTW, if you're interested in testing and use jMock you might want to check out JDave and its excellent Wicket support, http://www.jdave.org/ . Best wishes, Timo -- Timo Rantalaiho Reaktor Innovations OyURL: http://www.ri.fi/ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Using the WicketTester WITHOUT rendering a response on submit
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 MockModuleT extends AbstractModule{ private T mock; private ClassT type; public MockModule(ClassT 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 MockModulePartDAO(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()