You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Russell Brown <Ru...@ioko.com> on 2008/09/15 18:09:23 UTC

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


Re: T5: testing using PageTester and EasyMock

Posted by Angelo Chen <an...@yahoo.com.hk>.
Hi,

This will be a very interesting topic, I'd like to see how TDD can be used
in the development of T5 pages, my experience with T5 page tester isn't so
successful, now I try to do everything in the services as it is a easy place
to test.

angelo


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

-- 
View this message in context: http://www.nabble.com/T5%3A-testing-using-PageTester-and-EasyMock-tp19496126p19529056.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: T5: testing using PageTester and EasyMock

Posted by Angelo Chen <an...@yahoo.com.hk>.
Hi Russel,

Very interesting! possible to have a very simple sample project that get us
started? thanks.

Angelo


Russell Brown-6 wrote:
> 
> 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
> 
> 

-- 
View this message in context: http://www.nabble.com/T5%3A-testing-using-PageTester-and-EasyMock-tp19496126p19531051.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: T5: testing using PageTester and EasyMock

Posted by Russell Brown <Ru...@ioko.com>.
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:akochnev@gmail.com] 
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 <sd...@hotmail.com> 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:Russell.Brown@ioko.com]
> > 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: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
>
> --
> 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: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5: testing using PageTester and EasyMock

Posted by Alex Kotchnev <ak...@gmail.com>.
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 <sd...@hotmail.com> 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:Russell.Brown@ioko.com]
> > 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: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/T5%3A-testing-using-PageTester-and-EasyMock-tp19496126p19513126.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

RE: T5: testing using PageTester and EasyMock

Posted by SergeEby <sd...@hotmail.com>.
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:Russell.Brown@ioko.com] 
> 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: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/T5%3A-testing-using-PageTester-and-EasyMock-tp19496126p19513126.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: T5: testing using PageTester and EasyMock

Posted by Russell Brown <Ru...@ioko.com>.
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:Russell.Brown@ioko.com] 
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: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org