Pass ObjectFactory instance instead of class to TestNG

323 views
Skip to first unread message

David Garcia

unread,
Feb 11, 2010, 8:55:12 PM2/11/10
to testng-users
When using a custom ObjectFactory:

TestNG testng = new TestNG();
testng.setObjectFactory(MyObjectFactory.class)

this limits MyObjectFactory to have a default constructor only.

Is there a reason why I can't do this:

TestNG testng = new TestNG();
IObjectFactory myFactory = new MyObjectFactory();
testng.setObjectFactory(myFactory);

As an example, I was thinking about the GuiceObjectFactory from TestNG
book

public GuiceObjectFactory() {
injector = Guice.createInjector(this);
}

what if for some reason I would like to create my injector outside and
pass it to the factory through the constructor?

public GuiceObjectFactory(Injector injector)

this is just an example, but what if I would like to pass any other
param to the factory itself?
Any workarounds?

Thanks
David

David Garcia

unread,
Feb 11, 2010, 9:14:12 PM2/11/10
to testng-users

Forgot some details.

I guess I could use something like

class FactorySetup {

@ObjectFactory
public IObjectFactory createFactory() {

Startable service = new StartableService();
return new MyObjectFactory(something);

}

}

When and by who does FactorySetup gets instatiated?
How can I have control over its lifecycle?

I would Ideally want to have this:

Startable service = new StartableService();
service.start();

IObjectFactory myFactory = new MyObjectFactory(service);


TestNG testng = new TestNG();

testng.setObjectFactory(myFactory);
...
testn.run();
...
service.stop();

Kartik Kumar

unread,
Feb 12, 2010, 1:26:40 PM2/12/10
to testng...@googlegroups.com
I think you have to add FactorySetup as a test class.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


Cédric Beust ♔

unread,
Feb 12, 2010, 3:55:20 PM2/12/10
to testng...@googlegroups.com
Hi David,

Sure, I can add a setObjectFactory(IObjectFactory factory) method to the TestNG class, would that solve your problem?

-- 
Cédric



Kartik Kumar

unread,
Feb 12, 2010, 5:46:57 PM2/12/10
to testng...@googlegroups.com
Hi Cedric,

Will this also work with ANT execution? Currently I don't think there is a provision to add object factory as an ant argument.

Thanks,

Kartik

2010/2/12 Cédric Beust ♔ <cbe...@google.com>

Cédric Beust ♔

unread,
Feb 12, 2010, 5:53:05 PM2/12/10
to testng...@googlegroups.com
How would that work?  You can't pass an instance on the command line...

-- 
Cédric

Kartik Kumar

unread,
Feb 12, 2010, 5:59:46 PM2/12/10
to testng...@googlegroups.com
Hi Cedric,

I am sorry misread your email. You can set ObjectFactory programmatically to TestNG object. I do it all the time with PowerMock' object factory.

Cédric Beust ♔

unread,
Feb 12, 2010, 6:02:04 PM2/12/10
to testng...@googlegroups.com
On Fri, Feb 12, 2010 at 2:59 PM, Kartik Kumar <krishn...@gmail.com> wrote:
Hi Cedric,

I am sorry misread your email. You can set ObjectFactory programmatically to TestNG object. I do it all the time with PowerMock' object factory.

Yes, but until now, you could only set the ObjectFactory *class*, which means the only way to instantiate one is if the class has a no-arg constructor.  I just added a setter so you can now specify the instance of factory you want.

--
Cédric


David Garcia

unread,
Feb 12, 2010, 7:55:08 PM2/12/10
to testng-users
Cédric,

Yes, I think that's all I need and will solve my problem.
Thanks!

On Feb 12, 3:02 pm, Cédric Beust ♔ <cbe...@google.com> wrote:


> On Fri, Feb 12, 2010 at 2:59 PM, Kartik Kumar <krishnan.1...@gmail.com>wrote:
>
> > Hi Cedric,
>
> > I am sorry misread your email. You can set ObjectFactory programmatically
> > to TestNG object. I do it all the time with PowerMock' object factory.
>
> Yes, but until now, you could only set the ObjectFactory *class*, which
> means the only way to instantiate one is if the class has a no-arg
> constructor.  I just added a setter so you can now specify the instance of
> factory you want.
>
> --

> ***Cédric
> *

Reply all
Reply to author
Forward
0 new messages