Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create one Guice injector for all tests or suites #398

Open
avarabyeu opened this issue Jun 9, 2013 · 13 comments
Open

Unable to create one Guice injector for all tests or suites #398

avarabyeu opened this issue Jun 9, 2013 · 13 comments

Comments

@avarabyeu
Copy link

Unable to create one Guice injector for all tests or suites. I would suggest to introduce some GuiceInjectorFactory to be able to manage and cache injectors by yourself. Current functionality may be decoupled to some DefaultInjectorFactory or something like that. New factories may be accessible from org.testng.TestNG.

@cbeust
Copy link
Collaborator

cbeust commented Jun 10, 2013

IModuleFactory lets you return the module you want, just return the same one all the time and the same injector will be used to instantiate all your test classes.

Or am I missing something?

@avarabyeu
Copy link
Author

Absolutely right. But there is no possibility to share Injectors between tests. Each test creates new injector and puts it into test context. New test == new test context, so there is no way to cache injectors somewhere...
org.testng.internal.ClassImpl.getInstanceFromGuice()

One thing i've found out as workaround - we can create base class for all tests and inject class members from constructor.

public class BaseTest {
public BaseTest() {
GuiceInjector.getInstance().getInjector().injectMembers(this);
}
}

So, we can create some singleton with injector inside ('GuiceInjector') and use it for all tests.

@avarabyeu
Copy link
Author

Cedric, would you mind if I fix that? I can fork repo and provide solution.

@cbeust
Copy link
Collaborator

cbeust commented Jun 20, 2013

Sure, go ahead.

Cédric

On Thu, Jun 20, 2013 at 1:25 PM, Andrei Varabyeu
[email protected]:

Cedric, would you mind if I fix that? I can fork repo and provide solution.


Reply to this email directly or view it on GitHubhttps://github.com//issues/398#issuecomment-19780709
.

@usmanismail
Copy link

@avarabyeu Do you remember the Pull Request or Commit for this change?

@diegovegaz
Copy link

@avarabyeu, @cbeust
Hi
I'm facing the same issue where multiple injectors are getting created for different tests belonging to the same suite, and I need one injector instance for all tests.
Was this solution implemented?

@avarabyeu
Copy link
Author

@diegovegaz As i remember, i've just created injector manually and utilized it in ObjectFactory

@krmahadevan
Copy link
Member

@diegovegaz @avarabyeu - Would you be able to help share a sample that I can use to reproduce this issue ? Based on what I find, i can try addressing this issue or atleast coming back with a proper way of getting this done using TestNG as a sample.

@diegovegaz
Copy link

diegovegaz commented Feb 13, 2018

I have a testng.xml with the suite having multiple tests which are configured to run in parallel.
For all TestNG classes, there is a base class, which is annotated with @guice(module = xxx).

//Below is the base test class which uses the Guice module for DI

@Guice(modules = {XXXTestsGuiceModule.class})
public class XXXBaseTest{

    @Inject
    @Named("testObj")
    private TestObj testObj;

}

//Below is the module definition

public class XXXTestsGuiceModule extends AbstractModule{

    @Override
    protected void configure() {
    bind(TestObj.class)
                .annotatedWith(Names.named("testObj"))
                .to(TestObj.class)
                .in(Scopes.SINGLETON);
        }

}

//Below is the dependency

@Slf4j
public class TestObj {

    public TestObj() {
        log.debug("creating test object instance");
    }
}

When the tests start, I see the above log statement ("creating test object instance") printed more than once.

@diegovegaz
Copy link

From https://groups.google.com/d/msg/testng-users/5-Nso0oY0yY/uXZYb1wNw1AJ, found this mentioned by Cedric:

"Whenever you use a @guice annotation on one of your test classes, TestNG will create an injector with the modules your provided"

Can you let me know which version of TestNG has the fix? I am currently using 6.8

@diegovegaz
Copy link

@krmahadevan, shared the sample above, please let me know if you have a solution or workaround for the same.

@vlsi
Copy link
Contributor

vlsi commented May 22, 2021

@cbeust , sorry for the late mention, however, have you considered using scopes? (e.g. SuiteScope, TestScope)

For instance, the current IModuleFactory supplies ITestContext context, Class<?> testClass via factory arguments.

However, it could provide the very same information via Guice bindings. Then IModuleFactory won't really be needed.

WDYT?

@vlsi
Copy link
Contributor

vlsi commented May 24, 2021

This seems to be a relevant case: https://groups.google.com/g/testng-users/c/KWqo4l4PeU4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants