You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Mark W. Shead" <mw...@fas.harvard.edu> on 2009/08/25 16:46:46 UTC

Testify and Injecting Services from IOC Registry

The Testify documentation seems to imply that if its jar is present on  
your class path, you can use @Inject within your tests just like you  
would in a page class and get services from your IOC registry. (http://tapestry.formos.com/nightly/tapestry-testify/#Inject_services_into_tests 
).

However it doesn't seem to be working the way I expect.  Is there some  
other setup that needs to take place?


public class SimpleTest2 extends AbstractIntegrationTestSuite

{
	@Inject
	IDataSource datasource;

	@Test
	public void dataSourceTest() throws Exception {
		assertNotNull(datasource); // This fails in the test.
	}

}

datasource is null in this case, but I know it is in the registry  
correctly because I can use datasource injected like this within a  
page class.

Mark

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


Re: Testify and Injecting Services from IOC Registry

Posted by Paul Field <pa...@db.com>.
"Mark W. Shead" <mw...@gmail.com> wrote on 26/08/2009 15:58:05:
> I am using the Selenium testing so I'm subclassing from 
> AbstractIntegrationTestSuite in  tapestry-test.

Ahhh.... The key thing about the TapestryTest superclasses in Testify is 
that they orchestrate the annotation processing and test scope quite 
carefully. You can't use them in conjunction with the 
AbstractIntegrationTestSuite from tapestry-test (as you need to subclass 
both).

So, you'll need to write a custom superclass for your tests that extends 
AbstractIntegrationTestSuite and then make sure all your test classes 
extend it. Something like this:


public abstract class MyAbstractIntegrationTest extends 
AbstractIntegrationTestSuite {
    protected static final TapestryTester tester = new TapestryTester(
"com.myapp.tapestry5", "app", PageTester.DEFAULT_CONTEXT_PATH);
 
    @BeforeClass(alwaysRun=true)
    public final void processInjectAnnotation() {
        tester.injectInto(this);
    }

 
    @BeforeMethod(alwaysRun=true)
    public final void setUp() throws Exception {
          // Put any other general setup here such as: 
Mockito.initMocks(this)
        doSetUp();
        tester.collectForComponentsFrom(this);
    }


    /**
     * Override this instead of {@link #setUp()}
     */
    protected void doSetUp() throws Exception {
        // Do nothing by default
    }


    @AfterMethod
    public final void tearDown() throws Exception {
        tester.endTest();
    }
}

Let me know how that goes and I can add it to Testify as another 
integration.

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: Testify and Injecting Services from IOC Registry

Posted by "Mark W. Shead" <mw...@gmail.com>.
Paul,

I am using the Selenium testing so I'm subclassing from  
AbstractIntegrationTestSuite in  tapestry-test.  Is there a way to get  
the IOC Registry from there?

Mark

On Aug 26, 2009, at 4:47 AM, Paul Field wrote:

> Hi Mark,
>
> If you are subclassing from one of the TapestryTest classes in Testify
> then the @Inject annotation should be processed in your test.
> You can put a breakpoint into TapestryTester#injectInto(Object) to  
> check
> that the annotation is being processed.
>
> If that's happening then perhaps the IOC registry isn't being set up  
> as
> you expect - check how you create you TapestryTester object.
>
> Let me know if that doesn't help.
>
> - 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: Testify and Injecting Services from IOC Registry

Posted by "Mark W. Shead" <mw...@gmail.com>.
The code you sent does indeed appear to work.  Thank you very much for  
your help.

Mark

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


Re: Testify and Injecting Services from IOC Registry

Posted by Paul Field <pa...@db.com>.
"Mark W. Shead" <mw...@fas.harvard.edu> wrote on 28/08/2009 21:36:55:

> Hm it appears to give me a copy of the registry, but not the same 
> instance that was used for testing the pages.

I'm not too sure what you mean by "not the same instance that was used for 
testing the pages" but I'm going to guess that you are running some unit 
tests as well as some Selenium tests in the same process (test run).

If that's true, then my code will create a TapestryTester for all the 
selenium tests but you may already have a TapestryTester that is shared 
for all your unit tests. The TapestryTester contains a complete IOC so 
you'll have two IOCs at the same time (which may or may not be a problem).

There are two approaches to solving the problem.

The first is to point to point the selenium tests at the TapestryTester 
that you already have. So, instead of this from my code:

    public abstract class MyAbstractIntegrationTest extends
            AbstractIntegrationTestSuite {
        protected static final TapestryTester tester =
            new TapestryTester("com.myapp.tapestry5", "app", 
PageTester.DEFAULT_CONTEXT_PATH);
 
Use something like this instead:

    public abstract class MyAbstractIntegrationTest extends
            AbstractIntegrationTestSuite {
        protected static final TapestryTester tester = AbstractMyUnitTest.
SHARED_TESTER;

This has the advantage of creating just one IOC and sharing it - which is 
fine if you genuinely want the same IOC in both contexts.


The second option is to use TestNG's lifecycle annotations to control the 
scope of each of the IOCs. I suspect organising the selenium and the unit 
tests as two separate "test"s in TestNG and using the @BeforeTest and 
@AfterTest annotations might be the right thing:

    @BeforeTest(alwaysRun=true)
    public void startup() {
        tester = new TapestryTester(...);
    }


    @AfterTest(alwaysRun=true)
    public void shutdown() {
        tester.shutdown();
    }

You'll need one set of the two methods for your Selenium tests and another 
set for your unit tests. However, I'm not a TestNG expert so I might be on 
the wrong track with this suggestion.

- 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: Testify and Injecting Services from IOC Registry

Posted by "Mark W. Shead" <mw...@fas.harvard.edu>.
Paul,

Hm it appears to give me a copy of the registry, but not the same  
instance that was used for testing the pages.  For example, my tests  
open a file based database and use it for the web app.  At the end of  
the test I want to close it.  Using the code you sent, it appears that  
the other copy of the registry attempts to open this database before  
calling the close command.

Mark

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


Re: Testify and Injecting Services from IOC Registry

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

If you are subclassing from one of the TapestryTest classes in Testify 
then the @Inject annotation should be processed in your test.
You can put a breakpoint into TapestryTester#injectInto(Object) to check 
that the annotation is being processed.

If that's happening then perhaps the IOC registry isn't being set up as 
you expect - check how you create you TapestryTester object.

Let me know if that doesn't help.

- 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.