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 at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to