My first guess is that there are more than one injector around:
 - you create a second injector in your servlet config?
 - you have somewhere a static reference to the injector which is not reset when a second test is run

hope this helps




On 05/22/2013 07:51 PM, Grinder wrote:
Hi,

I'm writing a unit test that tries to extract and test data accumulated in a singleton DAO service after calling a servlet. I don't use Guice Web and so I inject the servlet manually. And in order to get the handle of the singleton DAO, I also inject it to the unit test itself. Here's how the setup looks like

interface DAO { ... }

@Singleton
class RealDAO implements DAO { ... }
@Singleton
class DummyDAO implements DAO { ... }

class MyRealModule extends AbstractModule {
  configure() {
     bind(DAO.class).to(RealDAO.class);
  }
}

class MyServlet {
    @Inject private DAO theDAO;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        Injector injector = (Injector) config.getServletContext().getAttribute(Injector.class.getName());
        injector.injectMembers(this);
    }
    ...
}

class TestServlet { // TestNG
  Injector injector;
  @Inject DAO tesDAO; // I don't have to make the servlet's dao public.
  MyServlet servlet;
  @BeforeMethod
  void setup() {
        injector = Guice.createInjector(Modules.override(new TestSettingModule()).with(new AbstractModule() {
          configure() {
            bind(DAO.class).to(DummyDAO.class).in(Singleton.class);
          }
        }));
       // set up a ServletConfig with the injector as servlet context attribute
       servlet = new MyServlet();
       servlet.init(config); // with the injector
       injector.injectMember(this); // inject to the test class itself
  }
  void testServlet() {
     servlet.doPOST();
     assertEqual(testDAO.getSomeValue(), expectedValue); // failed here!!!
  }
}

The testServlet failed because injector creates two different instances of DummyDAO and so the servlet is talking to one and the test is examing its own.

Why would injector create two instances for object annotated with Singleton scope? What's wrong in my setup.

Thanks!

            

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply via email to