Hi Alex,
Testing pages as POJOs is quite simple. You can roll your own very,
very, very simple service injector and inject EasyMock services into the
pojo. I have done it using reflection. So I have a class that takes the
page instance being tested in the constructor (and the test class too)
and for each field annotated with @Inject it creates a strict mock and
sets the field to that value. It also adds the mock to an internal List
if mocks. Then the class provides convenience replay, reset, verify
methods that iterate over the whole list. The test class also has fields
that use a custom annotation (@Mock) these are injected with the same
values as those added to the page under test. So you end up with a test
class with populated mocks, a page class with populated services and a
helper class with the same mocks held in an iterable fashion for
convenience.



Russell

-----Original Message-----
From: Alex Kotchnev [mailto:[EMAIL PROTECTED] 
Sent: 16 September 2008 17:39
To: Tapestry users
Subject: Re: T5: testing using PageTester and EasyMock

I was grappling with this issue myself, I still don't have a good answer
to
it. Because T5 is so heavily annotation driven (with the IoC and the
framework doing a lot of the magic heavy lifting behind the scenes),
testing
the pages as POJOs (e.g. setting some properties, performing an action,
inspecting the state of the page) is  not immediately obvious.

I've looked through some of the T5 unit tests (w/ EasyMock), and often
times
a page has a special (e.g. package private) method to inject services
that
otherwise the framework would inject. So, for example, if the page used
to
have an :

@Inject
FooService fooService

@Inject
BarService barService

Then, the page class would usually have a package private method like
this :


void setServices(FooService fs, BarService bs) {
   this.fooService = fs;
   this.barService = bs;
}

Anyway, there certainly is room for improvement, most advanced web
framework
have a way of testing pages/components as Pojos. Although the solution
above
works OK (and if you think about it, it's still pojos), it certainly
isn't
the first thing to think of once you get used to having Tapestry
injecting a
bunch of things into your pages. From a conceptual point of view, when
you're *unit *testing a page, you really DON'T want to have the "real"
services injected, and you'd probably just want to have
mocks/stubs/fakes in
their place that return the data needed to unit test the page. Now, if
you
were doing more of an "integration" type of test (e.g. where you test
how
the page works with the *real* service), then it's a different ball
game,
you do need the real services injected (for which you can still use the
above approach and not depend on the IoC to do it for you).

I guess the alternative would be to have a "test context" for binding
test/mock instances of the dependent services and somehow ask T5 to
inject
them into the page being tested. But then, it wouldn't really be unit
testing as the tests would depend on a whole bunch of things other than
the
unit being tested.

Let's keep this discussion rolling, we certainly need a little more info
on
testing in T5.

Cheers,

Alex Kotchnev

On Tue, Sep 16, 2008 at 10:32 AM, SergeEby <[EMAIL PROTECTED]> wrote:

>
> Hi,
>
> I had a similar question a few weeks ago and didn't get any response.
> Can someone else chime in?
> It would be nice to have "real world" example in the documentation to
> showcase TDD features of T5.
>
> /Serge
>
>
> Russell Brown-6 wrote:
> >
> > One more related question would be this: in the docs at
> > http://tapestry.apache.org/tapestry5/guide/unit-testing-pages.html
you
> > tell the PageTester class the name of your "filter" so it can load
your
> > module. But how can you tell the PageTester to use the Spring filter
not
> > the plain tapestry filter? All my services are Spring services so as
> > soon as I try and run a test I get a load of errors about no service
> > realizing interface XXX (which is the type of a field annotated with
> > @Inject).
> >
> > Any ideas on this one either?
> >
> > I'm having quite a hard time testing anything beyond the most
> > rudimentary. I notice that tap core and tap ioc have themselves very
> > high test coverage indeed.
> >
> > Cheers
> >
> > Russell
> >
> > -----Original Message-----
> > From: Russell Brown [mailto:[EMAIL PROTECTED]
> > Sent: 15 September 2008 17:09
> > To: Tapestry users
> > Subject: T5: testing using PageTester and EasyMock
> >
> > Hi,
> >
> > Does anyone have any ideas for testing components using mock
services? I
> > can create a MyAppMockModule class and use that to configure the
page
> > tester. This class builds mocks of my services, so far so good. I
use
> > PageTester.getRegistry() and then
Registry.getService(MyService.class)
> > and then I have my mock to set up in my test. No? No! I have a proxy
> > wrapped instance instead which I can't use at all.
> >
> >
> >
> > Any ideas out there? How are you testing your components pages when
you
> > have expense to construct services with complex external
dependencies?
> >
> >
> >
> > Cheers
> >
> >
> >
> > Russell
> >
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
>
> --
> View this message in context:
>
http://www.nabble.com/T5%3A-testing-using-PageTester-and-EasyMock-tp1949
6126p19513126.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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]

Reply via email to