Hi Juan,

> I am trying to inject a service to one component using testifiy's
> @ForComponent annotation and Mockito but the component doesn't get the
> mocked service injected.

I think you have invalidated the warranty on Testify ;-)

It is important to make sure your tests are setup using the inheritance 
hierarchy described in the Setup instructions:
http://tapestry.formos.com/nightly/tapestry-testify/#Setup

And then in your "AbstractMyApplicationTest" class you need to override 
setUpForAllTestMethods() as described in the Mockito instructions:
http://tapestry.formos.com/nightly/tapestry-testify/#Mockito

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

And if you need any other setup per-method then it's best to override 
doSetUp() rather than using @BeforeMethod.

Basically, there's a very specific sequence processing lifecycle that 
needs to be followed for all the Testify features to work in a seamless 
way. The "TapestryTest" base classes, use of an application-specific base 
class and use of template methods (setUpForAllTestMethods()) is all 
carefully designed to make this happen smoothly.



So, for example, you have tried to use @BeforeMethod to initialise the 
Mocks:

>    @ForComponents
>    @Mock
>    UserFactory _userFactory;
> 
>    @BeforeMethod
>    public void initTests(){
>       MockitoAnnotations.initMocks(this);
>    } 

But, unfortunately, TestNG is now in charge of the lifecycle and it will 
get it wrong :-( It will initialise the mock *after* it processes 
@ForComponents - so that's why there is no mock for the component (the 
field is still 'null' when @ForComponents is processed).


FYI, I am interested if anyone has any ideas for a less 
inheritance-oriented approach to solving the lifecycle orchestration - 
I've thought about it and I can't see a better way with TestNG and, 
although JUnit 4 has Runners (@RunWith), I don't think that solves the 
whole problem.

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.

Reply via email to