Not sure if I follow since I'm already creating a Guice Module and starting it in the Test class. Let's see if I can summarize it better:
1) TemplateModule binds an implementing class bind(Template.class).to(SomeTemplate.class); 2) created a TemplateModule and initialized it in the Test Class injector = Guice.createInjector(new TemplateModule.Builder().withSession(new Session()).buildModule()); 3) The same Test class has a @inject Template template Why is it not injected? >From your answer, it looks like I have to create one module per class where @inject is present. That seems incorrect to me. On Mar 2, 7:15 pm, Sam Berlin <[email protected]> wrote: > There's no instruction to the Injector telling it to Inject the > instance of your TemplateTest class. If you add an additional module > to the Injector, something like: > > new AbstractModule() { public void configure() { requestInjection(this); } } > > then Guice will know to process @Inject methods/variables within > TemplateTest too. > > Sam > > > > On Tue, Mar 2, 2010 at 6:40 PM, infinity2heaven <[email protected]> > wrote: > > New to guice. > > > public interface Template { > > foo(); > > } > > > public class SomeTemplate implements Template { > > �...@inject > > public SomeTemplate (Session session){ > > this.session = session; > > } > > > public foo() { > > // do something > > } > > } > > > // Module with bindings > > public class TemplateModule extends AbstractModule { > > > �...@override > > protected void configure() { > > bind(Session.class).toInstance(session); > > bind(Template.class).to(SomeTemplate.class); > > } > > > public static class Builder { > > private Session session; > > > public TemplateModule buildModule() { > > return new TemplateModule(session); > > } > > > public Builder withSession(String session) { > > this.session = session; > > return this; > > } > > } > > } > > > // JUnt 4.4 test > > public class TemplateTest { > > > �...@inject > > Template template; // doesn't inject! > > > �...@beforeclass > > public static void init() throws Exception { > > injector = Guice.createInjector(new > > TemplateModule.Builder().withSession(new Session())); > > // template= injector.getInstance(Template.class); // > > template is > > initialized if this is uncommented > > } > > > �...@test > > public void testFoo() { > > template.foo(); // NullPointerException > > } > > > } > > > What is wrong with the above code? Why doesn't @Inject automatically > > inject the template instance variable? It seems redundant (and > > limited) that I have to yet again use Injector.getInstance, each time > > I need to access the template object. I'm sure doing something wrong > > here ... > > > -- > > 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 > > athttp://groups.google.com/group/google-guice?hl=en. -- 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.
