Dependency Injection question

27 views
Skip to first unread message

Wujek Srujek

unread,
Dec 23, 2009, 10:36:37 AM12/23/09
to testng-users
Hello.

I am a recent convert to TestNG. First of all, thank you for such a
great job with this test framework, it is very helpful for us in the
new project we are developing.

One of the features we like and rely on is Dependency Injection. I
have 2 qustions related to it:
1. We create an expensive object in @BeforeSuite, place it in an
injected ITestContext object, and then, each test class that needs
this expensive-to-create object has ITestContext injected into
@BeforeClass (it actually is injected in a superclass, but it's not
the case here), looks it up and can use it any test methods. Is it
guaranted that the ITestContext object injected to any of the classes
in a given suite will be the same instance (it is a TestRunner type),
and hence it is guaranteed that the attribute will be there waiting
for us? (It looks like it, but I would like to make sure.)
2. The documentation on the web site says that:
"Any @Before method or @Test method can declare a parameter of type
ITestContext."
However, I was able to inject ITestContext to a @AfterSuite method, as
I want to lookup this expensive object and deinitialize it. As such
use is not described (or at least I couldn't find it), I would like to
ask if this is just an overlookng in the docs, or am I using something
that is not guaranteed and likely to change in the future?

Lastly, Marry Christmass and a Happy New Year!

Wujek.

Cédric Beust ♔

unread,
Dec 23, 2009, 5:15:28 PM12/23/09
to testng...@googlegroups.com
Hi Wujek,

On Wed, Dec 23, 2009 at 7:36 AM, Wujek Srujek <wujek....@googlemail.com> wrote:
Hello.

I am a recent convert to TestNG. First of all, thank you for such a
great job with this test framework, it is very helpful for us in the
new project we are developing.

One of the features we like and rely on is Dependency Injection. I
have 2 qustions related to it:
1. We create an expensive object in @BeforeSuite, place it in an
injected ITestContext object, and then, each test class that needs
this expensive-to-create object has ITestContext injected into
@BeforeClass (it actually is injected in a superclass, but it's not
the case here), looks it up and can use it any test methods. Is it
guaranted that the ITestContext object injected to any of the classes
in a given suite will be the same instance (it is a TestRunner type),
and hence it is guaranteed that the attribute will be there waiting
for us? (It looks like it, but I would like to make sure.)

Yes.

 
2. The documentation on the web site says that:
"Any @Before method or @Test method can declare a parameter of type
ITestContext."
However, I was able to inject ITestContext to a @AfterSuite method, as
I want to lookup this expensive object and deinitialize it. As such
use is not described (or at least I couldn't find it), I would like to
ask if this is just an overlookng in the docs, or am I using something
that is not guaranteed and likely to change in the future?


This won't change, I should update the documentation:  this injection can be done on all the configuration methods, not juste @Before.
 
Lastly, Marry Christmass and a Happy New Year!


Thanks, same to you!

--
Cédric


Reply all
Reply to author
Forward
0 new messages