You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Mats Henricson <ma...@henricson.se> on 2009/09/02 14:27:12 UTC

Can't get Testify & JUnit 4 & Mockito working

Hi!

I've spent some time now trying to get Testify working.
I do it according to the documentation, with an abstract
base class like this:

public abstract class AbstractPageTest extends TapestryTest {

    private static final String APP_PACKAGE = "com.expekt.web";
    private static final TapestryTester SHARED_TESTER =
        new TapestryTester(APP_PACKAGE, AppModule.class);

    public AbstractPageTest() {
        super(SHARED_TESTER);
    }

    @Override
    protected void setUpForAllTestMethods() throws Exception {
        MockitoAnnotations.initMocks(this);
    }
}

I then have a subclass with the JUnit 4 test:

public class GamesLauncherTest extends AbstractPageTest {

    @ForComponents @Mock
    private XyzManager xyzManager;

    @Mock
    private XyzConfig xyzConfig;

    // ...

    private void tellMockitoHowToMock() {
        when(xyzManager.getConfig()).thenReturn(xyzConfig);
        // ...
    }

    @Test
    public void testGamesLauncher() {
        tellMockitoHowToMock();

        // Now lets render the page and see that we got the right thing
        Document page =
           tester.renderPage(GamesLauncher.class.getCanonicalName());
        Assert.assertNotNull(page.getElementById("xyz"));
    }
}

What I get is runtime exceptions, such as this:

Caused by: java.lang.RuntimeException: Service id 'xyzConfig' is
not defined by any module.  Defined services: AccessDecisionManager,
... VirtualAssetStreamer.
	at
org.apache.tapestry5.ioc.internal.RegistryImpl.locateModuleForService(RegistryImpl.java:351)
	at
org.apache.tapestry5.ioc.internal.RegistryImpl.getService(RegistryImpl.java:318)
	at
org.apache.tapestry5.ioc.internal.ObjectLocatorImpl.getService(ObjectLocatorImpl.java:39)
	at
org.apache.tapestry5.internal.services.ServiceAnnotationObjectProvider.provide(ServiceAnnotationObjectProvider.java:35)
	at
org.apache.tapestry5.ioc.internal.services.MasterObjectProviderImpl$1.invoke(MasterObjectProviderImpl.java:48)
	at
org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:68)
	... 188 more

We define our services in spring XML files, and @Inject them into our
pages, and this worries me quite a bit, since I can't see any examples
where Spring XML wiring has been mocked.

Or is that irrelevant?

Mats

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


Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Mats Henricson <ma...@henricson.se>.
It seems like Testify can't handle that I have my page in a
subdirectory of pages!

If I have my class in com.acme.web.pages.games, then I get
the exception below. If I put the class directly under pages
(com.acme.web.pages), then it works. Anyone have an idea
why this is the case?

Well, it doesn't work yet, I need to further mock the
behaviour of HttpServletRequest in contributeRequestHandler().

Mats

> Hm... it seems to be related to the fact that I had forgotten
> to have a contributeComponentClassResolver() in my PageTestModule
> class. So now my copied MessagePage test works! Woohoo!
> 
> But I still get this exception for my real page:
> 
>    Request was not handled:
>        'GamesLauncher' may not be a valid page name.
> 
> Need to read more on how contributeComponentClassResolver() works.
> 
> Mats
> 
>> Hi!
>>
>> Yes, this did indeed get me further down the road. Thanks a lot!
>> Now, when I run, I get a RunTimeException:
>>
>> Request was not handled:
>>    'MessagePage' may not be a valid page name.
>> 	at org.apache.tapestry5.test.PageTester.renderPage(PageTester.java:177)
>>
>> I'm looking into this, but haven't found how to fix it yet.
>>
>> Mats
>>
>>> Hi Mats,
>>>
>>> Yes, this is a question that has come up before, Paul provided the answer... you can do a google for the original post, but you will need to create a module with a contribution that ensures there are replacement HTTP request/response objects available for your tests, then use a mock to replace the request and response in your actual test.
>>>
>>> This is the code Paul provided:
>>> public static void
>>> contributeRequestHandler(OrderedConfiguration<RequestFilter> config, final
>>> RequestGlobals requestGlobals) {
>>>         RequestFilter filter = new RequestFilter() {
>>>             public boolean service(Request request, Response response,
>>> RequestHandler handler) throws IOException {
>>>                 requestGlobals.storeServletRequestResponse(mock
>>> (HttpServletRequest.class), mock(HttpServletResponse.class));
>>>                 return handler.service(request, response);
>>>             }
>>>         };
>>>         config.add("EnsureNonNullHttpRequestAndResponse", filter,
>>> "before:*");
>>>     }
>>>
>>> Kind regards,
>>> Peter
>>>
>>>
>>> ----- Original Message -----
>>> From: "Mats Henricson" <ma...@henricson.se>
>>> To: "Tapestry users" <us...@tapestry.apache.org>
>>> Sent: Thursday, 3 September, 2009 16:30:10 GMT +02:00 Athens, Beirut, Bucharest, Istanbul
>>> Subject: Re: Can't get Testify & JUnit 4 & Mockito working
>>>
>>>>> My current guess is that @ForComponents is pulling in and handling
>>>>> binding from my real AppManager, even though it is in no way part
>>>>> of the test.
>>>> In that case try creating the TapestryTester with a nonsense app name:
>>>>
>>>>    private static final TapestryTester SHARED_TESTER
>>>>        = new TapestryTester("nonsense", MyModule.class);
>>>>
>>>> That will make sure Tapestry/Testify are using only the modules you 
>>>> specify in the constructor.
>>> This seems to do the trick, actually! Much much thanks!
>>>
>>> But now Acegi Security is kicking in, and I get:
>>>
>>>    Can only process HttpServletRequest
>>>
>>> Has anyone managed to mock it out of the picture?
>>>
>>>
>>> Mats

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


Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Mats Henricson <ma...@henricson.se>.
Hm... it seems to be related to the fact that I had forgotten
to have a contributeComponentClassResolver() in my PageTestModule
class. So now my copied MessagePage test works! Woohoo!

But I still get this exception for my real page:

   Request was not handled:
       'GamesLauncher' may not be a valid page name.

Need to read more on how contributeComponentClassResolver() works.

Mats

> Hi!
> 
> Yes, this did indeed get me further down the road. Thanks a lot!
> Now, when I run, I get a RunTimeException:
> 
> Request was not handled:
>    'MessagePage' may not be a valid page name.
> 	at org.apache.tapestry5.test.PageTester.renderPage(PageTester.java:177)
> 
> I'm looking into this, but haven't found how to fix it yet.
> 
> Mats
> 
>> Hi Mats,
>>
>> Yes, this is a question that has come up before, Paul provided the answer... you can do a google for the original post, but you will need to create a module with a contribution that ensures there are replacement HTTP request/response objects available for your tests, then use a mock to replace the request and response in your actual test.
>>
>> This is the code Paul provided:
>> public static void
>> contributeRequestHandler(OrderedConfiguration<RequestFilter> config, final
>> RequestGlobals requestGlobals) {
>>         RequestFilter filter = new RequestFilter() {
>>             public boolean service(Request request, Response response,
>> RequestHandler handler) throws IOException {
>>                 requestGlobals.storeServletRequestResponse(mock
>> (HttpServletRequest.class), mock(HttpServletResponse.class));
>>                 return handler.service(request, response);
>>             }
>>         };
>>         config.add("EnsureNonNullHttpRequestAndResponse", filter,
>> "before:*");
>>     }
>>
>> Kind regards,
>> Peter
>>
>>
>> ----- Original Message -----
>> From: "Mats Henricson" <ma...@henricson.se>
>> To: "Tapestry users" <us...@tapestry.apache.org>
>> Sent: Thursday, 3 September, 2009 16:30:10 GMT +02:00 Athens, Beirut, Bucharest, Istanbul
>> Subject: Re: Can't get Testify & JUnit 4 & Mockito working
>>
>>>> My current guess is that @ForComponents is pulling in and handling
>>>> binding from my real AppManager, even though it is in no way part
>>>> of the test.
>>> In that case try creating the TapestryTester with a nonsense app name:
>>>
>>>    private static final TapestryTester SHARED_TESTER
>>>        = new TapestryTester("nonsense", MyModule.class);
>>>
>>> That will make sure Tapestry/Testify are using only the modules you 
>>> specify in the constructor.
>> This seems to do the trick, actually! Much much thanks!
>>
>> But now Acegi Security is kicking in, and I get:
>>
>>    Can only process HttpServletRequest
>>
>> Has anyone managed to mock it out of the picture?
>>
>>
>> Mats
>>
>> ---------------------------------------------------------------------
>> 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
>>
>>
> 
> 
> ---------------------------------------------------------------------
> 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: Can't get Testify & JUnit 4 & Mockito working

Posted by Mats Henricson <ma...@henricson.se>.
Hi!

Yes, this did indeed get me further down the road. Thanks a lot!
Now, when I run, I get a RunTimeException:

Request was not handled:
   'MessagePage' may not be a valid page name.
	at org.apache.tapestry5.test.PageTester.renderPage(PageTester.java:177)

I'm looking into this, but haven't found how to fix it yet.

Mats

> Hi Mats,
> 
> Yes, this is a question that has come up before, Paul provided the answer... you can do a google for the original post, but you will need to create a module with a contribution that ensures there are replacement HTTP request/response objects available for your tests, then use a mock to replace the request and response in your actual test.
> 
> This is the code Paul provided:
> public static void
> contributeRequestHandler(OrderedConfiguration<RequestFilter> config, final
> RequestGlobals requestGlobals) {
>         RequestFilter filter = new RequestFilter() {
>             public boolean service(Request request, Response response,
> RequestHandler handler) throws IOException {
>                 requestGlobals.storeServletRequestResponse(mock
> (HttpServletRequest.class), mock(HttpServletResponse.class));
>                 return handler.service(request, response);
>             }
>         };
>         config.add("EnsureNonNullHttpRequestAndResponse", filter,
> "before:*");
>     }
> 
> Kind regards,
> Peter
> 
> 
> ----- Original Message -----
> From: "Mats Henricson" <ma...@henricson.se>
> To: "Tapestry users" <us...@tapestry.apache.org>
> Sent: Thursday, 3 September, 2009 16:30:10 GMT +02:00 Athens, Beirut, Bucharest, Istanbul
> Subject: Re: Can't get Testify & JUnit 4 & Mockito working
> 
>>> My current guess is that @ForComponents is pulling in and handling
>>> binding from my real AppManager, even though it is in no way part
>>> of the test.
>> In that case try creating the TapestryTester with a nonsense app name:
>>
>>    private static final TapestryTester SHARED_TESTER
>>        = new TapestryTester("nonsense", MyModule.class);
>>
>> That will make sure Tapestry/Testify are using only the modules you 
>> specify in the constructor.
> 
> This seems to do the trick, actually! Much much thanks!
> 
> But now Acegi Security is kicking in, and I get:
> 
>    Can only process HttpServletRequest
> 
> Has anyone managed to mock it out of the picture?
> 
> 
> Mats
> 
> ---------------------------------------------------------------------
> 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
> 
> 


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


Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Peter Stavrinides <P....@albourne.com>.
Hi Mats,

Yes, this is a question that has come up before, Paul provided the answer... you can do a google for the original post, but you will need to create a module with a contribution that ensures there are replacement HTTP request/response objects available for your tests, then use a mock to replace the request and response in your actual test.

This is the code Paul provided:
public static void
contributeRequestHandler(OrderedConfiguration<RequestFilter> config, final
RequestGlobals requestGlobals) {
        RequestFilter filter = new RequestFilter() {
            public boolean service(Request request, Response response,
RequestHandler handler) throws IOException {
                requestGlobals.storeServletRequestResponse(mock
(HttpServletRequest.class), mock(HttpServletResponse.class));
                return handler.service(request, response);
            }
        };
        config.add("EnsureNonNullHttpRequestAndResponse", filter,
"before:*");
    }

Kind regards,
Peter


----- Original Message -----
From: "Mats Henricson" <ma...@henricson.se>
To: "Tapestry users" <us...@tapestry.apache.org>
Sent: Thursday, 3 September, 2009 16:30:10 GMT +02:00 Athens, Beirut, Bucharest, Istanbul
Subject: Re: Can't get Testify & JUnit 4 & Mockito working

>> My current guess is that @ForComponents is pulling in and handling
>> binding from my real AppManager, even though it is in no way part
>> of the test.
> 
> In that case try creating the TapestryTester with a nonsense app name:
> 
>    private static final TapestryTester SHARED_TESTER
>        = new TapestryTester("nonsense", MyModule.class);
> 
> That will make sure Tapestry/Testify are using only the modules you 
> specify in the constructor.

This seems to do the trick, actually! Much much thanks!

But now Acegi Security is kicking in, and I get:

   Can only process HttpServletRequest

Has anyone managed to mock it out of the picture?


Mats

---------------------------------------------------------------------
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: Can't get Testify & JUnit 4 & Mockito working

Posted by Mats Henricson <ma...@henricson.se>.
>> My current guess is that @ForComponents is pulling in and handling
>> binding from my real AppManager, even though it is in no way part
>> of the test.
> 
> In that case try creating the TapestryTester with a nonsense app name:
> 
>    private static final TapestryTester SHARED_TESTER
>        = new TapestryTester("nonsense", MyModule.class);
> 
> That will make sure Tapestry/Testify are using only the modules you 
> specify in the constructor.

This seems to do the trick, actually! Much much thanks!

But now Acegi Security is kicking in, and I get:

   Can only process HttpServletRequest

Has anyone managed to mock it out of the picture?


Mats

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


Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Peter Stavrinides <P....@albourne.com>.
Hi Mats,

I have checked your configuration and it looks correct, I am using JUnit4 and have it working with this same setup... one thing to be conscious of is using the correct service scope... you are aware that you may not be using TestifyConstants.PERTEST if you source in from AppModule?

regards,
Peter


----- Original Message -----
From: "Paul Field" <pa...@db.com>
To: "Tapestry users" <us...@tapestry.apache.org>
Sent: Thursday, 3 September, 2009 14:54:55 GMT +02:00 Athens, Beirut, Bucharest, Istanbul
Subject: Re: Can't get Testify & JUnit 4 & Mockito working

Mats Henricson <ma...@henricson.se> wrote on 03/09/2009 12:28:23:

> My current guess is that @ForComponents is pulling in and handling
> binding from my real AppManager, even though it is in no way part
> of the test.

In that case try creating the TapestryTester with a nonsense app name:

   private static final TapestryTester SHARED_TESTER
       = new TapestryTester("nonsense", MyModule.class);

That will make sure Tapestry/Testify are using only the modules you 
specify in the constructor.

Paul





---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.

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


Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Paul Field <pa...@db.com>.
Mats Henricson <ma...@henricson.se> wrote on 03/09/2009 12:28:23:

> My current guess is that @ForComponents is pulling in and handling
> binding from my real AppManager, even though it is in no way part
> of the test.

In that case try creating the TapestryTester with a nonsense app name:

   private static final TapestryTester SHARED_TESTER
       = new TapestryTester("nonsense", MyModule.class);

That will make sure Tapestry/Testify are using only the modules you 
specify in the constructor.

Paul





---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.

Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Mats Henricson <ma...@henricson.se>.
>>> Caused by: java.lang.RuntimeException: Service id 'xyzConfig' is
>>> not defined by any module.  Defined services: AccessDecisionManager,
>>> ... VirtualAssetStreamer.
>> Something is trying to look up a service with id "xyzConfig" - using that 
>> id. I assume you have something like this in your page:
>>
>>    @Inject
>>    @Service("xyzConfig")
>>    private XyzConfig config;
>>
>> If that's true then it might simply be that you haven't got a 
>> @ForComponents annotation on your xyzConfig mock. Also, if you name the 
>> service (using the @Service annotation) then you need to name it in the 
>> @ForComponents too:
>>
>> public class GamesLauncherTest extends AbstractPageTest {
>>
>>     @ForComponents @Mock
>>     private XyzManager xyzManager;
>>  
>>     @ForComponents("xyzConfig") @Mock
>>     private XyzConfig xyzConfig;
> 
> OK, I'll try that.

That didn't work either.

So now I've started out copy paste of example code to my code base,
to see if the MessagePage and ForComponentsJustMockTest examples
would work, and they don't. Same error message.

My current guess is that @ForComponents is pulling in and handling
binding from my real AppManager, even though it is in no way part
of the test.

Does anyone have a clue?

Mats


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


Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Mats Henricson <ma...@henricson.se>.
> Hi Mats,

Hi!

>> I then have a subclass with the JUnit 4 test:
>>
>> public class GamesLauncherTest extends AbstractPageTest {
>>
>>     @ForComponents @Mock
>>     private XyzManager xyzManager;
>>
>>     @Mock
>>     private XyzConfig xyzConfig;
>>
>>
>> What I get is runtime exceptions, such as this:
>>
>> Caused by: java.lang.RuntimeException: Service id 'xyzConfig' is
>> not defined by any module.  Defined services: AccessDecisionManager,
>> ... VirtualAssetStreamer.
> 
> Something is trying to look up a service with id "xyzConfig" - using that 
> id. I assume you have something like this in your page:
> 
>    @Inject
>    @Service("xyzConfig")
>    private XyzConfig config;
> 
> If that's true then it might simply be that you haven't got a 
> @ForComponents annotation on your xyzConfig mock. Also, if you name the 
> service (using the @Service annotation) then you need to name it in the 
> @ForComponents too:
> 
> public class GamesLauncherTest extends AbstractPageTest {
> 
>     @ForComponents @Mock
>     private XyzManager xyzManager;
>  
>     @ForComponents("xyzConfig") @Mock
>     private XyzConfig xyzConfig;

OK, I'll try that.

>> We define our services in spring XML files, and @Inject them into our
>> pages, and this worries me quite a bit, since I can't see any examples
>> where Spring XML wiring has been mocked.
> 
> If you are faking/mocking the services then there's no problem. However, 
> Testify and Tapestry's PageTester don't work with the tapestry-spring 
> integration so you can't rely on the real spring services for (for 
> example) integration tests.

Actually, I don't want the Spring wiring at all. I hope to be able
to use Mockito mocks all the way.

Mats


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


Re: Can't get Testify & JUnit 4 & Mockito working

Posted by Paul Field <pa...@db.com>.
Hi Mats,

> I then have a subclass with the JUnit 4 test:
> 
> public class GamesLauncherTest extends AbstractPageTest {
> 
>     @ForComponents @Mock
>     private XyzManager xyzManager;
> 
>     @Mock
>     private XyzConfig xyzConfig;
> 
> 
> What I get is runtime exceptions, such as this:
> 
> Caused by: java.lang.RuntimeException: Service id 'xyzConfig' is
> not defined by any module.  Defined services: AccessDecisionManager,
> ... VirtualAssetStreamer.

Something is trying to look up a service with id "xyzConfig" - using that 
id. I assume you have something like this in your page:

   @Inject
   @Service("xyzConfig")
   private XyzConfig config;

If that's true then it might simply be that you haven't got a 
@ForComponents annotation on your xyzConfig mock. Also, if you name the 
service (using the @Service annotation) then you need to name it in the 
@ForComponents too:

public class GamesLauncherTest extends AbstractPageTest {

    @ForComponents @Mock
    private XyzManager xyzManager;
 
    @ForComponents("xyzConfig") @Mock
    private XyzConfig xyzConfig;


> We define our services in spring XML files, and @Inject them into our
> pages, and this worries me quite a bit, since I can't see any examples
> where Spring XML wiring has been mocked.

If you are faking/mocking the services then there's no problem. However, 
Testify and Tapestry's PageTester don't work with the tapestry-spring 
integration so you can't rely on the real spring services for (for 
example) integration tests.

- Paul




---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.