More than one injector created by GuiceBerry

130 views
Skip to first unread message

Georgi Tsankov

unread,
Jun 23, 2011, 7:53:36 PM6/23/11
to guiceber...@googlegroups.com
Below is a simplified version of the code I'm dealing with (hopefully I haven't missed anything crucial):

class Test extends junit.framework.TestCase {
  @Inject Repo repo;

  setUp() {
    ...
    AutoTearDownGuiceBerry.setUp(this, TestEnv.class);
    ...
  }

  test() {
    ...
    // run X in a new thread
    ...
  }
}

class X {
  @Inject Repo repo;

  doStuff () {
    // do something with repo.
  }
}

The problem is: X.repo and Test.repo are different instances, even though Repo is @Singleton in it's module... so my conclusion is there are (at least) two injectors here.

I printed the stack trace inside Repo's constructor:
  - The one created for X:
      at Repo.<init>(Repo.java:47)
      at RepoModule.<clinit>(RepoModule.java:30)
      // Java reflection stuff, invoke(), etc.
      at com.google.inject.internal.ProviderMethod.get(ProviderMethod.java:104)
      ...
      at com.google.guiceberry.GuiceBerryUniverse$TestCaseScaffolding.runBeforeTest(GuiceBerryUniverse.java:99)  // injector = getAndSetInjector(gbeClass);
      ...
      AutoTearDownGuiceBerry.setUp()

  - The one created for Test:
      at Repo.<init>(Repo.java:47)
      at Repo$$FastClassByGuice$$af0939b1.newInstance(<generated>)
      at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)
      ...
      (GuiceBerryUniverse.java:171)  // injector.injectMembers(testDescription.getTestCase());
      (GuiceBerryUniverse.java:117)  // injectMembersIntoTest(gbeClass, injector);
      ...
      AutoTearDownGuiceBerry.setUp()


The question is, how to access the instance of X.repo inside Test as well?
I need to initialize it inside the test case before X.doStuff() is run.

I tried @Inject Injector injector inside Test, and then injector.getInstance(Repo.class), but I got the same result.

Thank you.

Georgi

Luiz-Otavio Zorzella

unread,
Jun 23, 2011, 8:14:57 PM6/23/11
to guiceber...@googlegroups.com
There's almost always (see note below) 2 injectors when running
GuiceBerry tests -- an Injector created by GuiceBerry, which has
bindings for your test (e.g. WebDriver/Selenium), and your regular
server's injector. See "server tutorial" which discusses that at
length.

It's sometimes desirable to get ahold of your server injector's
bindings from your tests -- and, if server and test run in the same
JVM it's also quite easy to do so. I will eventually write about how
to do it elegantly, but, if you want to just hack it, store your
server injector in a public static Injector SERVER_INJECTOR variable
somewhere, and, from your tests, do
SERVER_INJECTOR.getInstance(Foo.class). Again, there are more elegant
ways of doing it...

Note: you *may* have a single injector (the GuiceBerry-created one),
but I won't really describe here how to do it -- you either know how,
or you probably don't want to...

Peace,

Z

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

Georgi Tsankov

unread,
Jun 28, 2011, 4:09:16 PM6/28/11
to guiceber...@googlegroups.com
Thank you for the quick response.
I looked more closely into the tutorials, and tried several things...
What eventually worked was:
  - in Test.setUp(): added repo = Guice.createInjector(new RepoModule()).getInstance(Repo.class); I had to do this _before_ calling AutoTearDownGuiceBerry.setUp(). This repo is now the same as X.repo!
  - in RepoModule: added a static member and inside the configure() method: bind(Repo.class).toInstance(<that static member>);
I forgot to mention that there were multiple servers all using the same module, so the code in my previous post was incomplete... but the problem was reproducible even with one server.

best,
Georgi
Reply all
Reply to author
Forward
0 new messages