I've come up with a possible solution to this issue or at least a start that
can be discussed a bit more.

My solution is to implement my own implementation of wicket's IPageFactory. 
This implementation is really just a wrapper around the default one since
the default one is final.

I then created a simple singleton class that allows developers to call
mockPage(MyPage.class) within their unit tests.  The singleton just keeps a
map of page classes that should be mocked up.  One thing you need to
remember is to provide a way to clear the map after a test runs.

I then created a Page object called MockPage that takes the class of the
page being mocked and a corresponding .html that contains no markup. 

private final Class<?> mockedPageClass;

public MockPage(Class<?> mockedPageClass) {
    this.mockedPageClass = mockedPageClass;
}

Then within the IPageFactory methods I check if the pageClass parameter is
mockable given the singleton and return an instance of MockPage if it is, if
not then I just delegate to the default factory.

Then finally I created an extension to WicketTester and overrode the
assertRenderedPage method to first check if the last page rendered is an
instance of MockPage and if so then compare the pageClass parameter to the
mockedPageClass that was set on the MockPage.  If the last page rendered is
not of instance MockPage then simply delegate to your parent method.

The only other thing you need to do is to set the new IPageFactory up in the
application being used my your WicketTester.

Thoughts?

-Craig


Ingram Chen-2 wrote:
> 
> We also suffer the same issues here. But due to unmanaged nature of
> Wicket,
> there is no chance to intercept construction of page B unless you build
> your
> own factory for page.
> 
> class Page A {
>      MyFactory myFactory ;
>      public Page A {
>         add(new Link("toBPage") {
>              setResponsePage(myFactory.newBPage());
>         });
>      }
> }
> 
> and swapping mock MyFactory while testing.
> 
> But such extra indirection make code slight complex and MyFactory is still
> hard to test, either.
> 
> 
> On 7/17/07, Craig Lenzen <[EMAIL PROTECTED]> wrote:
>>
>>
>> I'm looking for some feedback as to an issue I'm having with the
>> WicketTester.  To start I'd like to point out that I'm using Spring and
>> injecting my pages / components via the wicket spring project's component
>> injector.
>>
>> Here is the situation, I have page A that has a link to page B.  In the
>> test
>> of page A I test that the click in fact goes to page B.  This is fine but
>> the problem is, is that page B relies on a Spring service during its
>> construction and the fact that the WicketTester actually tries to render
>> page B which calls the service.  The easy fix to this is to simply create
>> a
>> mock implementation of that service and set it in the mock context when
>> testing page A, and don't forget you need to also setup the expected
>> calls
>>
>> and returns.
>>
>> The problem here is that fact that I'm only testing page A, I don't care
>> about the functionality of page B nor which services it might call.  So
>> is
>> there a better way?  Is there a way that you can mock the rendering of
>> page
>> B?  Has anyone else ran into this issue and  or questioned it, or has
>> someone came up with a solution?
>>
>> To get a little more advanced you could also say that when testing a Page
>> that added a number of panels I don't want those panels to render during
>> the
>> testing of the page, I only want to know the panels where added to the
>> page.
>>
>> Thanks for everyone's help,
>> Craig
>> --
>> View this message in context:
>> http://www.nabble.com/WicketTester-and-mocking-up-next-page-rendered.-tf4093923.html#a11641094
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by DB2 Express
>> Download DB2 Express C - the FREE version of DB2 express and take
>> control of your XML. No limits. Just data. Click to get it now.
>> http://sourceforge.net/powerbar/db2/
>> _______________________________________________
>> Wicket-user mailing list
>> Wicket-user@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/wicket-user
>>
> 
> 
> 
> -- 
> Ingram Chen
> online share order: http://dinbendon.net
> blog: http://www.javaworld.com.tw/roller/page/ingramchen
> 
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Wicket-user mailing list
> Wicket-user@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/wicket-user
> 
> 

-- 
View this message in context: 
http://www.nabble.com/WicketTester-and-mocking-up-next-page-rendered.-tf4093923.html#a11715146
Sent from the Wicket - User mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to