I've seen a couple of posts mentioning the warning "Multiple injectors
detected" from guice-servlet, but they're either related to Struts2
integration or incorrectly configured modules. None of the posts
explain what exactly the message means in a unit test environment (if
it means anything at all), and if there is a *nice* solution...

The "Multiple injectors detected" warnings are distracting me from
real warnings and errors, so I was hoping I can get rid of it...

I'm using guice 2.0 and guice-servlet 2.0, and no Struts. I have
subclassed ServletModule, and that module is loaded whenever I create
an injector; in particular in my unit tests @Before. Whenever I run
multiple unit tests at once, GuiceFilter.setPipeline() logs this
warning:

"Multiple injectors detected. Please install only one ServletModule in
your web application. While you may have more than one injector, you
should only configure guice-servlet in one of them. (Hint: look for
legacy ServetModules or multiple calls to Servlets.configure())."

The following test illustrates the 'problem':

public class GuiceWarningTest {
    private Injector injector;

    @Before
    public void setUp() {
        System.out.println("setUp()");
        injector = Guice.createInjector(new ServletModule());
    }

    @After
    public void tearDown() {
        System.out.println("tearDown()");
        injector = null;
        // Clean up static stuff on the GuiceFilter that Guice
complains about when creating 2 injectors in 1 JVM
//        new GuiceFilter().destroy();
    }

    @Test
    public void testNumber1() {
        System.out.println("testNumber1()");
    }

    @Test
    public void testNumber2() {
        System.out.println("testNumber2()");
    }
}

Result:
  setUp()
  testNumber1()
  tearDown()
  setUp()
  Jan 26, 2010 9:23:58 PM com.google.inject.servlet.GuiceFilter
setPipeline
  WARNING: Multiple injectors detected. (...)
  testNumber2()
  tearDown()

(When I call new GuiceFilter().destroy() in the @After, the error
message disappears.)

Isn't GuiceFilter.setPipeline() drawing the wrong conclusion (or
logging a misleading warning)?
    if (GuiceFilter.pipeline instanceof ManagedFilterPipeline) {
      if (Stage.PRODUCTION.equals(stage)) {
        throw new RuntimeException(MULTIPLE_INJECTORS_ERROR);
      } else {
        Logger.getLogger(GuiceFilter.class.getName()).warning
(MULTIPLE_INJECTORS_ERROR);
      }
    }

When the second run of @Before creates the second injector instance,
the one created during the
first run of @Before may already have been garbage-collected, and the
two injectors possibly never co-exist. So no multiple injectors were
detected, it's just that the static pipeline was set more than once...

Questions:
[1] What exactly can go wrong, if I don't destroy the GuiceFilter in
@After in the above situation?
[2] Is there a possibility to get rid of this warning if it doesn't
really reflect what's going on, and can be ignored safely in the first
place? (I mean solve in GuiceFilter, not 'hiding' it by lowering my
log level;)
[3] Is there a less ugly way to work around this issue than doing "new
GuiceFilter().destroy()" in the @After of every unit test?
[4] The comment in GuiceFilter.setPipeline() says
    // tests that don't have a tearDown that calls GuiceFilter#reset
().
It should be
    // tests that don't have a tearDown that calls "new GuiceFilter
().destroy()"

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

Reply via email to