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 at 
> http://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.

Reply via email to