Wicket + Guice + unittests

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Wicket + Guice + unittests

Daniel Watrous-2
Hi,

I've integrated Guice into Wicket successfully, but I'm struggling
with the unittests. I'm not sure how to get the injector into my
HomePage class. Here's my setup.

I'm using GuiceFilter with a GuiceServletContextListener. That creates
the injector and a ServletModule which defines the WicketApplication.
I followed:
http://code.google.com/p/google-guice/wiki/ServletModule

Here's some of MyGuiceServletConfig extends GuiceServletContextListener

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(createServletModule(), new
MongoHoneybadgerModule());
    }

    private ServletModule createServletModule() {
        return new ServletModule() {
...

In my WicketApplication extends WebApplication I have this init() method

    @Override
    public void init()
    {
        super.init();
        Injector bootStrapInjector = (Injector)
this.getServletContext().getAttribute(Injector.class.getName());
        getComponentInstantiationListeners().add(new
GuiceComponentInjector(this, bootStrapInjector));
    }

Now, in my HomePage.java class I have

public class HomePage extends WebPage {
    private static final long serialVersionUID = 1L;
    @Inject private Injector injector;

    public HomePage(final PageParameters parameters) {
        super(parameters);
        SomeType myobj = injector.getInstance(SomeType.class);

        add(new Label("version", myobj.getValue()));
    }
}

This all runs great inside a web container as a servlet.

The PROBLEM: I'm getting a NullPointerException on the line where I
reference the injector:
        SomeType myobj = injector.getInstance(SomeType.class);

My test class is what was generated by the wicket quickstart. I'm not
sure how to make an injector available in setUp.

    @Before
    public void setUp() {
        tester = new WicketTester(new WicketApplication());
    }

Any ideas?

Thanks,
Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Ronan O'Connell
Hi Daniel,

I'm using Guice in a couple of wicket projects though my understanding
of it is a little limited! My set-up matches yours except that in my
unit test setup I call injectMembers :

     @Before
     public void setUp()
     {
         final StubProjectorApplication stubApplication = new
StubProjectorApplication();
         _tester = new WicketTester(stubApplication);
         stubApplication.getWarpInjector().injectMembers(this); //
getWarpInjector returns the injector built in the init method
     }

I'm not sure this is right..it feels to me that it shouldn't be be
necessary, but it works for me :) .

Ronan

On 10/10/2012 18:30, Daniel Watrous wrote:

> Hi,
>
> I've integrated Guice into Wicket successfully, but I'm struggling
> with the unittests. I'm not sure how to get the injector into my
> HomePage class. Here's my setup.
>
> I'm using GuiceFilter with a GuiceServletContextListener. That creates
> the injector and a ServletModule which defines the WicketApplication.
> I followed:
> http://code.google.com/p/google-guice/wiki/ServletModule
>
> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
>
>      @Override
>      protected Injector getInjector() {
>          return Guice.createInjector(createServletModule(), new
> MongoHoneybadgerModule());
>      }
>
>      private ServletModule createServletModule() {
>          return new ServletModule() {
> ...
>
> In my WicketApplication extends WebApplication I have this init() method
>
>      @Override
>      public void init()
>      {
>          super.init();
>          Injector bootStrapInjector = (Injector)
> this.getServletContext().getAttribute(Injector.class.getName());
>          getComponentInstantiationListeners().add(new
> GuiceComponentInjector(this, bootStrapInjector));
>      }
>
> Now, in my HomePage.java class I have
>
> public class HomePage extends WebPage {
>      private static final long serialVersionUID = 1L;
>      @Inject private Injector injector;
>
>      public HomePage(final PageParameters parameters) {
> super(parameters);
>          SomeType myobj = injector.getInstance(SomeType.class);
>
> add(new Label("version", myobj.getValue()));
>      }
> }
>
> This all runs great inside a web container as a servlet.
>
> The PROBLEM: I'm getting a NullPointerException on the line where I
> reference the injector:
>          SomeType myobj = injector.getInstance(SomeType.class);
>
> My test class is what was generated by the wicket quickstart. I'm not
> sure how to make an injector available in setUp.
>
>      @Before
>      public void setUp() {
>          tester = new WicketTester(new WicketApplication());
>      }
>
> Any ideas?
>
> Thanks,
> Daniel
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

robmcguinness
Not sure if this helps but this an unfinished older Wicket project that I setup using Guice.

https://github.com/robmcguinness/inception/tree/master/src/main/java/com/robmcguinness/web/application
Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Daniel Watrous-2
In reply to this post by Ronan O'Connell
Thanks Ronan,

Can you share the implementation of StubProjectorApplication? That
doesn't appear to be part of Wicket or Guice.

Daniel

On Wed, Oct 10, 2012 at 12:29 PM, Ronan O'Connell
<[hidden email]> wrote:

> Hi Daniel,
>
> I'm using Guice in a couple of wicket projects though my understanding of it
> is a little limited! My set-up matches yours except that in my unit test
> setup I call injectMembers :
>
>     @Before
>     public void setUp()
>     {
>         final StubProjectorApplication stubApplication = new
> StubProjectorApplication();
>         _tester = new WicketTester(stubApplication);
>         stubApplication.getWarpInjector().injectMembers(this); //
> getWarpInjector returns the injector built in the init method
>     }
>
> I'm not sure this is right..it feels to me that it shouldn't be be
> necessary, but it works for me :) .
>
> Ronan
>
>
> On 10/10/2012 18:30, Daniel Watrous wrote:
>>
>> Hi,
>>
>> I've integrated Guice into Wicket successfully, but I'm struggling
>> with the unittests. I'm not sure how to get the injector into my
>> HomePage class. Here's my setup.
>>
>> I'm using GuiceFilter with a GuiceServletContextListener. That creates
>> the injector and a ServletModule which defines the WicketApplication.
>> I followed:
>> http://code.google.com/p/google-guice/wiki/ServletModule
>>
>> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
>>
>>      @Override
>>      protected Injector getInjector() {
>>          return Guice.createInjector(createServletModule(), new
>> MongoHoneybadgerModule());
>>      }
>>
>>      private ServletModule createServletModule() {
>>          return new ServletModule() {
>> ...
>>
>> In my WicketApplication extends WebApplication I have this init() method
>>
>>      @Override
>>      public void init()
>>      {
>>          super.init();
>>          Injector bootStrapInjector = (Injector)
>> this.getServletContext().getAttribute(Injector.class.getName());
>>          getComponentInstantiationListeners().add(new
>> GuiceComponentInjector(this, bootStrapInjector));
>>      }
>>
>> Now, in my HomePage.java class I have
>>
>> public class HomePage extends WebPage {
>>      private static final long serialVersionUID = 1L;
>>      @Inject private Injector injector;
>>
>>      public HomePage(final PageParameters parameters) {
>>         super(parameters);
>>          SomeType myobj = injector.getInstance(SomeType.class);
>>
>>         add(new Label("version", myobj.getValue()));
>>      }
>> }
>>
>> This all runs great inside a web container as a servlet.
>>
>> The PROBLEM: I'm getting a NullPointerException on the line where I
>> reference the injector:
>>          SomeType myobj = injector.getInstance(SomeType.class);
>>
>> My test class is what was generated by the wicket quickstart. I'm not
>> sure how to make an injector available in setUp.
>>
>>      @Before
>>      public void setUp() {
>>          tester = new WicketTester(new WicketApplication());
>>      }
>>
>> Any ideas?
>>
>> Thanks,
>> Daniel
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Dan Retzlaff
In reply to this post by Daniel Watrous-2
Daniel,

What you're doing should work, but I think you're giving
your GuiceComponentInjector a null Injector. Unit tests don't go through
web.xml to set up its context listeners, so
your GuiceServletContextListener never has a chance to construct and
register an Injector with the ServletContext.

Dan

On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <[hidden email]>wrote:

> Hi,
>
> I've integrated Guice into Wicket successfully, but I'm struggling
> with the unittests. I'm not sure how to get the injector into my
> HomePage class. Here's my setup.
>
> I'm using GuiceFilter with a GuiceServletContextListener. That creates
> the injector and a ServletModule which defines the WicketApplication.
> I followed:
> http://code.google.com/p/google-guice/wiki/ServletModule
>
> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
>
>     @Override
>     protected Injector getInjector() {
>         return Guice.createInjector(createServletModule(), new
> MongoHoneybadgerModule());
>     }
>
>     private ServletModule createServletModule() {
>         return new ServletModule() {
> ...
>
> In my WicketApplication extends WebApplication I have this init() method
>
>     @Override
>     public void init()
>     {
>         super.init();
>         Injector bootStrapInjector = (Injector)
> this.getServletContext().getAttribute(Injector.class.getName());
>         getComponentInstantiationListeners().add(new
> GuiceComponentInjector(this, bootStrapInjector));
>     }
>
> Now, in my HomePage.java class I have
>
> public class HomePage extends WebPage {
>     private static final long serialVersionUID = 1L;
>     @Inject private Injector injector;
>
>     public HomePage(final PageParameters parameters) {
>         super(parameters);
>         SomeType myobj = injector.getInstance(SomeType.class);
>
>         add(new Label("version", myobj.getValue()));
>     }
> }
>
> This all runs great inside a web container as a servlet.
>
> The PROBLEM: I'm getting a NullPointerException on the line where I
> reference the injector:
>         SomeType myobj = injector.getInstance(SomeType.class);
>
> My test class is what was generated by the wicket quickstart. I'm not
> sure how to make an injector available in setUp.
>
>     @Before
>     public void setUp() {
>         tester = new WicketTester(new WicketApplication());
>     }
>
> Any ideas?
>
> Thanks,
> Daniel
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Martin Grigorov-4
Hi,

https://github.com/apache/wicket/blob/master/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java
The code doesn't look very nice, IMO, but it should help you start your tests.

On Thu, Oct 11, 2012 at 3:10 AM, Dan Retzlaff <[hidden email]> wrote:

> Daniel,
>
> What you're doing should work, but I think you're giving
> your GuiceComponentInjector a null Injector. Unit tests don't go through
> web.xml to set up its context listeners, so
> your GuiceServletContextListener never has a chance to construct and
> register an Injector with the ServletContext.
>
> Dan
>
> On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <[hidden email]>wrote:
>
>> Hi,
>>
>> I've integrated Guice into Wicket successfully, but I'm struggling
>> with the unittests. I'm not sure how to get the injector into my
>> HomePage class. Here's my setup.
>>
>> I'm using GuiceFilter with a GuiceServletContextListener. That creates
>> the injector and a ServletModule which defines the WicketApplication.
>> I followed:
>> http://code.google.com/p/google-guice/wiki/ServletModule
>>
>> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
>>
>>     @Override
>>     protected Injector getInjector() {
>>         return Guice.createInjector(createServletModule(), new
>> MongoHoneybadgerModule());
>>     }
>>
>>     private ServletModule createServletModule() {
>>         return new ServletModule() {
>> ...
>>
>> In my WicketApplication extends WebApplication I have this init() method
>>
>>     @Override
>>     public void init()
>>     {
>>         super.init();
>>         Injector bootStrapInjector = (Injector)
>> this.getServletContext().getAttribute(Injector.class.getName());
>>         getComponentInstantiationListeners().add(new
>> GuiceComponentInjector(this, bootStrapInjector));
>>     }
>>
>> Now, in my HomePage.java class I have
>>
>> public class HomePage extends WebPage {
>>     private static final long serialVersionUID = 1L;
>>     @Inject private Injector injector;
>>
>>     public HomePage(final PageParameters parameters) {
>>         super(parameters);
>>         SomeType myobj = injector.getInstance(SomeType.class);
>>
>>         add(new Label("version", myobj.getValue()));
>>     }
>> }
>>
>> This all runs great inside a web container as a servlet.
>>
>> The PROBLEM: I'm getting a NullPointerException on the line where I
>> reference the injector:
>>         SomeType myobj = injector.getInstance(SomeType.class);
>>
>> My test class is what was generated by the wicket quickstart. I'm not
>> sure how to make an injector available in setUp.
>>
>>     @Before
>>     public void setUp() {
>>         tester = new WicketTester(new WicketApplication());
>>     }
>>
>> Any ideas?
>>
>> Thanks,
>> Daniel
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>



--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Daniel Watrous-2
In reply to this post by Dan Retzlaff
Dan,

I think you're right. Since in the WicketApplication init() function I
attempt to get the bootStrapInjector like this:
        Injector bootStrapInjector = (Injector)
this.getServletContext().getAttribute(Injector.class.getName());

I just can't figure out how to get the injector into the
ServletContext before init() is run in my WicketApplication.

Daniel

On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]> wrote:

> Daniel,
>
> What you're doing should work, but I think you're giving
> your GuiceComponentInjector a null Injector. Unit tests don't go through
> web.xml to set up its context listeners, so
> your GuiceServletContextListener never has a chance to construct and
> register an Injector with the ServletContext.
>
> Dan
>
> On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <[hidden email]>wrote:
>
>> Hi,
>>
>> I've integrated Guice into Wicket successfully, but I'm struggling
>> with the unittests. I'm not sure how to get the injector into my
>> HomePage class. Here's my setup.
>>
>> I'm using GuiceFilter with a GuiceServletContextListener. That creates
>> the injector and a ServletModule which defines the WicketApplication.
>> I followed:
>> http://code.google.com/p/google-guice/wiki/ServletModule
>>
>> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
>>
>>     @Override
>>     protected Injector getInjector() {
>>         return Guice.createInjector(createServletModule(), new
>> MongoHoneybadgerModule());
>>     }
>>
>>     private ServletModule createServletModule() {
>>         return new ServletModule() {
>> ...
>>
>> In my WicketApplication extends WebApplication I have this init() method
>>
>>     @Override
>>     public void init()
>>     {
>>         super.init();
>>         Injector bootStrapInjector = (Injector)
>> this.getServletContext().getAttribute(Injector.class.getName());
>>         getComponentInstantiationListeners().add(new
>> GuiceComponentInjector(this, bootStrapInjector));
>>     }
>>
>> Now, in my HomePage.java class I have
>>
>> public class HomePage extends WebPage {
>>     private static final long serialVersionUID = 1L;
>>     @Inject private Injector injector;
>>
>>     public HomePage(final PageParameters parameters) {
>>         super(parameters);
>>         SomeType myobj = injector.getInstance(SomeType.class);
>>
>>         add(new Label("version", myobj.getValue()));
>>     }
>> }
>>
>> This all runs great inside a web container as a servlet.
>>
>> The PROBLEM: I'm getting a NullPointerException on the line where I
>> reference the injector:
>>         SomeType myobj = injector.getInstance(SomeType.class);
>>
>> My test class is what was generated by the wicket quickstart. I'm not
>> sure how to make an injector available in setUp.
>>
>>     @Before
>>     public void setUp() {
>>         tester = new WicketTester(new WicketApplication());
>>     }
>>
>> Any ideas?
>>
>> Thanks,
>> Daniel
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Dan Retzlaff
For what it's worth, we instantiate our applications through Guice. Having
your application "go get" its Injector kind of violates the DI concept.

filter("/*").through(WicketFilter.class);
bind(WebApplication.class).to(CustomWebApplication.class);
bind(WicketFilter.class).to(CustomWicketFilter.class);

@Singleton
private static class CustomFilter extends WicketFilter {
@Inject private Provider<WebApplication> webApplicationProvider;
 @Override
protected IWebApplicationFactory getApplicationFactory() {
return new IWebApplicationFactory() {
@Override
public WebApplication createApplication(WicketFilter filter) {
return webApplicationProvider.get();
}
@Override
public void destroy(WicketFilter filter) {
}
};
}
}

On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
<[hidden email]>wrote:

> Dan,
>
> I think you're right. Since in the WicketApplication init() function I
> attempt to get the bootStrapInjector like this:
>         Injector bootStrapInjector = (Injector)
> this.getServletContext().getAttribute(Injector.class.getName());
>
> I just can't figure out how to get the injector into the
> ServletContext before init() is run in my WicketApplication.
>
> Daniel
>
> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]> wrote:
> > Daniel,
> >
> > What you're doing should work, but I think you're giving
> > your GuiceComponentInjector a null Injector. Unit tests don't go through
> > web.xml to set up its context listeners, so
> > your GuiceServletContextListener never has a chance to construct and
> > register an Injector with the ServletContext.
> >
> > Dan
> >
> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
> [hidden email]>wrote:
> >
> >> Hi,
> >>
> >> I've integrated Guice into Wicket successfully, but I'm struggling
> >> with the unittests. I'm not sure how to get the injector into my
> >> HomePage class. Here's my setup.
> >>
> >> I'm using GuiceFilter with a GuiceServletContextListener. That creates
> >> the injector and a ServletModule which defines the WicketApplication.
> >> I followed:
> >> http://code.google.com/p/google-guice/wiki/ServletModule
> >>
> >> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
> >>
> >>     @Override
> >>     protected Injector getInjector() {
> >>         return Guice.createInjector(createServletModule(), new
> >> MongoHoneybadgerModule());
> >>     }
> >>
> >>     private ServletModule createServletModule() {
> >>         return new ServletModule() {
> >> ...
> >>
> >> In my WicketApplication extends WebApplication I have this init() method
> >>
> >>     @Override
> >>     public void init()
> >>     {
> >>         super.init();
> >>         Injector bootStrapInjector = (Injector)
> >> this.getServletContext().getAttribute(Injector.class.getName());
> >>         getComponentInstantiationListeners().add(new
> >> GuiceComponentInjector(this, bootStrapInjector));
> >>     }
> >>
> >> Now, in my HomePage.java class I have
> >>
> >> public class HomePage extends WebPage {
> >>     private static final long serialVersionUID = 1L;
> >>     @Inject private Injector injector;
> >>
> >>     public HomePage(final PageParameters parameters) {
> >>         super(parameters);
> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >>
> >>         add(new Label("version", myobj.getValue()));
> >>     }
> >> }
> >>
> >> This all runs great inside a web container as a servlet.
> >>
> >> The PROBLEM: I'm getting a NullPointerException on the line where I
> >> reference the injector:
> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >>
> >> My test class is what was generated by the wicket quickstart. I'm not
> >> sure how to make an injector available in setUp.
> >>
> >>     @Before
> >>     public void setUp() {
> >>         tester = new WicketTester(new WicketApplication());
> >>     }
> >>
> >> Any ideas?
> >>
> >> Thanks,
> >> Daniel
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Daniel Watrous-2
Dan,

I'm not talking about my application. I'm talking about unittests.
I've followed the Guice recommended way to integrate with servlets
using the GuiceFilter.

Now I'm trying to make the Wicket unittests work and I need the
injector to be available in WicketTester.

Daniel

On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]> wrote:

> For what it's worth, we instantiate our applications through Guice. Having
> your application "go get" its Injector kind of violates the DI concept.
>
> filter("/*").through(WicketFilter.class);
> bind(WebApplication.class).to(CustomWebApplication.class);
> bind(WicketFilter.class).to(CustomWicketFilter.class);
>
> @Singleton
> private static class CustomFilter extends WicketFilter {
> @Inject private Provider<WebApplication> webApplicationProvider;
>  @Override
> protected IWebApplicationFactory getApplicationFactory() {
> return new IWebApplicationFactory() {
> @Override
> public WebApplication createApplication(WicketFilter filter) {
> return webApplicationProvider.get();
> }
> @Override
> public void destroy(WicketFilter filter) {
> }
> };
> }
> }
>
> On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
> <[hidden email]>wrote:
>
>> Dan,
>>
>> I think you're right. Since in the WicketApplication init() function I
>> attempt to get the bootStrapInjector like this:
>>         Injector bootStrapInjector = (Injector)
>> this.getServletContext().getAttribute(Injector.class.getName());
>>
>> I just can't figure out how to get the injector into the
>> ServletContext before init() is run in my WicketApplication.
>>
>> Daniel
>>
>> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]> wrote:
>> > Daniel,
>> >
>> > What you're doing should work, but I think you're giving
>> > your GuiceComponentInjector a null Injector. Unit tests don't go through
>> > web.xml to set up its context listeners, so
>> > your GuiceServletContextListener never has a chance to construct and
>> > register an Injector with the ServletContext.
>> >
>> > Dan
>> >
>> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
>> [hidden email]>wrote:
>> >
>> >> Hi,
>> >>
>> >> I've integrated Guice into Wicket successfully, but I'm struggling
>> >> with the unittests. I'm not sure how to get the injector into my
>> >> HomePage class. Here's my setup.
>> >>
>> >> I'm using GuiceFilter with a GuiceServletContextListener. That creates
>> >> the injector and a ServletModule which defines the WicketApplication.
>> >> I followed:
>> >> http://code.google.com/p/google-guice/wiki/ServletModule
>> >>
>> >> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
>> >>
>> >>     @Override
>> >>     protected Injector getInjector() {
>> >>         return Guice.createInjector(createServletModule(), new
>> >> MongoHoneybadgerModule());
>> >>     }
>> >>
>> >>     private ServletModule createServletModule() {
>> >>         return new ServletModule() {
>> >> ...
>> >>
>> >> In my WicketApplication extends WebApplication I have this init() method
>> >>
>> >>     @Override
>> >>     public void init()
>> >>     {
>> >>         super.init();
>> >>         Injector bootStrapInjector = (Injector)
>> >> this.getServletContext().getAttribute(Injector.class.getName());
>> >>         getComponentInstantiationListeners().add(new
>> >> GuiceComponentInjector(this, bootStrapInjector));
>> >>     }
>> >>
>> >> Now, in my HomePage.java class I have
>> >>
>> >> public class HomePage extends WebPage {
>> >>     private static final long serialVersionUID = 1L;
>> >>     @Inject private Injector injector;
>> >>
>> >>     public HomePage(final PageParameters parameters) {
>> >>         super(parameters);
>> >>         SomeType myobj = injector.getInstance(SomeType.class);
>> >>
>> >>         add(new Label("version", myobj.getValue()));
>> >>     }
>> >> }
>> >>
>> >> This all runs great inside a web container as a servlet.
>> >>
>> >> The PROBLEM: I'm getting a NullPointerException on the line where I
>> >> reference the injector:
>> >>         SomeType myobj = injector.getInstance(SomeType.class);
>> >>
>> >> My test class is what was generated by the wicket quickstart. I'm not
>> >> sure how to make an injector available in setUp.
>> >>
>> >>     @Before
>> >>     public void setUp() {
>> >>         tester = new WicketTester(new WicketApplication());
>> >>     }
>> >>
>> >> Any ideas?
>> >>
>> >> Thanks,
>> >> Daniel
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: [hidden email]
>> >> For additional commands, e-mail: [hidden email]
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Dan Retzlaff
I follow you. WicketTester doesn't know about GuiceFilter, so you'll need a
different way of getting your Injector into your Wicket Application. Rather
than getting the Injector from your servlet context attributes, I'm
suggesting that you let Guice instantiate your Application so you can
@Inject the injector like any other dependency. The binding code I posted
previously does the (non-test) setup; for the test itself it's as simple as
https://gist.github.com/3880246.

Hope that helps. By the way, I enjoyed your Wicket+EC2 article. Thanks for
that. :)

On Fri, Oct 12, 2012 at 4:08 PM, Daniel Watrous <[hidden email]>wrote:

> Dan,
>
> I'm not talking about my application. I'm talking about unittests.
> I've followed the Guice recommended way to integrate with servlets
> using the GuiceFilter.
>
> Now I'm trying to make the Wicket unittests work and I need the
> injector to be available in WicketTester.
>
> Daniel
>
> On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]> wrote:
> > For what it's worth, we instantiate our applications through Guice.
> Having
> > your application "go get" its Injector kind of violates the DI concept.
> >
> > filter("/*").through(WicketFilter.class);
> > bind(WebApplication.class).to(CustomWebApplication.class);
> > bind(WicketFilter.class).to(CustomWicketFilter.class);
> >
> > @Singleton
> > private static class CustomFilter extends WicketFilter {
> > @Inject private Provider<WebApplication> webApplicationProvider;
> >  @Override
> > protected IWebApplicationFactory getApplicationFactory() {
> > return new IWebApplicationFactory() {
> > @Override
> > public WebApplication createApplication(WicketFilter filter) {
> > return webApplicationProvider.get();
> > }
> > @Override
> > public void destroy(WicketFilter filter) {
> > }
> > };
> > }
> > }
> >
> > On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
> > <[hidden email]>wrote:
> >
> >> Dan,
> >>
> >> I think you're right. Since in the WicketApplication init() function I
> >> attempt to get the bootStrapInjector like this:
> >>         Injector bootStrapInjector = (Injector)
> >> this.getServletContext().getAttribute(Injector.class.getName());
> >>
> >> I just can't figure out how to get the injector into the
> >> ServletContext before init() is run in my WicketApplication.
> >>
> >> Daniel
> >>
> >> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
> wrote:
> >> > Daniel,
> >> >
> >> > What you're doing should work, but I think you're giving
> >> > your GuiceComponentInjector a null Injector. Unit tests don't go
> through
> >> > web.xml to set up its context listeners, so
> >> > your GuiceServletContextListener never has a chance to construct and
> >> > register an Injector with the ServletContext.
> >> >
> >> > Dan
> >> >
> >> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
> >> [hidden email]>wrote:
> >> >
> >> >> Hi,
> >> >>
> >> >> I've integrated Guice into Wicket successfully, but I'm struggling
> >> >> with the unittests. I'm not sure how to get the injector into my
> >> >> HomePage class. Here's my setup.
> >> >>
> >> >> I'm using GuiceFilter with a GuiceServletContextListener. That
> creates
> >> >> the injector and a ServletModule which defines the WicketApplication.
> >> >> I followed:
> >> >> http://code.google.com/p/google-guice/wiki/ServletModule
> >> >>
> >> >> Here's some of MyGuiceServletConfig extends
> GuiceServletContextListener
> >> >>
> >> >>     @Override
> >> >>     protected Injector getInjector() {
> >> >>         return Guice.createInjector(createServletModule(), new
> >> >> MongoHoneybadgerModule());
> >> >>     }
> >> >>
> >> >>     private ServletModule createServletModule() {
> >> >>         return new ServletModule() {
> >> >> ...
> >> >>
> >> >> In my WicketApplication extends WebApplication I have this init()
> method
> >> >>
> >> >>     @Override
> >> >>     public void init()
> >> >>     {
> >> >>         super.init();
> >> >>         Injector bootStrapInjector = (Injector)
> >> >> this.getServletContext().getAttribute(Injector.class.getName());
> >> >>         getComponentInstantiationListeners().add(new
> >> >> GuiceComponentInjector(this, bootStrapInjector));
> >> >>     }
> >> >>
> >> >> Now, in my HomePage.java class I have
> >> >>
> >> >> public class HomePage extends WebPage {
> >> >>     private static final long serialVersionUID = 1L;
> >> >>     @Inject private Injector injector;
> >> >>
> >> >>     public HomePage(final PageParameters parameters) {
> >> >>         super(parameters);
> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >> >>
> >> >>         add(new Label("version", myobj.getValue()));
> >> >>     }
> >> >> }
> >> >>
> >> >> This all runs great inside a web container as a servlet.
> >> >>
> >> >> The PROBLEM: I'm getting a NullPointerException on the line where I
> >> >> reference the injector:
> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >> >>
> >> >> My test class is what was generated by the wicket quickstart. I'm not
> >> >> sure how to make an injector available in setUp.
> >> >>
> >> >>     @Before
> >> >>     public void setUp() {
> >> >>         tester = new WicketTester(new WicketApplication());
> >> >>     }
> >> >>
> >> >> Any ideas?
> >> >>
> >> >> Thanks,
> >> >> Daniel
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: [hidden email]
> >> >> For additional commands, e-mail: [hidden email]
> >> >>
> >> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Daniel Watrous-2
Dan,

Thanks. I've got unittests running now, but the WicketFilter doesn't
seem to be processing. All I get when I run the applicaiton shows a
jetty produced directory listing.

In the snippet you provided before I think that CustomFilter and
CustomeWicketFilter should be the same thing. Is that right?

In my previous approach, when I bound the WicketFilter I included some
parameters, like this:
        filter("/*").through(WicketFilter.class,
createWicketFilterInitParams());

With this function

    private Map<String, String> createWicketFilterInitParams() {
        Map<String, String> wicketFilterParams = new HashMap<String, String>();
        wicketFilterParams.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
        wicketFilterParams.put("applicationClassName",
"com.hp.honeybadger.web.WicketApplication");
        return wicketFilterParams;
    }

I'm now trying to figure out how to make sure that the wicket filter
is called...

Daniel

On Fri, Oct 12, 2012 at 11:03 AM, Dan Retzlaff <[hidden email]> wrote:

> I follow you. WicketTester doesn't know about GuiceFilter, so you'll need a
> different way of getting your Injector into your Wicket Application. Rather
> than getting the Injector from your servlet context attributes, I'm
> suggesting that you let Guice instantiate your Application so you can
> @Inject the injector like any other dependency. The binding code I posted
> previously does the (non-test) setup; for the test itself it's as simple as
> https://gist.github.com/3880246.
>
> Hope that helps. By the way, I enjoyed your Wicket+EC2 article. Thanks for
> that. :)
>
> On Fri, Oct 12, 2012 at 4:08 PM, Daniel Watrous <[hidden email]>wrote:
>
>> Dan,
>>
>> I'm not talking about my application. I'm talking about unittests.
>> I've followed the Guice recommended way to integrate with servlets
>> using the GuiceFilter.
>>
>> Now I'm trying to make the Wicket unittests work and I need the
>> injector to be available in WicketTester.
>>
>> Daniel
>>
>> On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]> wrote:
>> > For what it's worth, we instantiate our applications through Guice.
>> Having
>> > your application "go get" its Injector kind of violates the DI concept.
>> >
>> > filter("/*").through(WicketFilter.class);
>> > bind(WebApplication.class).to(CustomWebApplication.class);
>> > bind(WicketFilter.class).to(CustomWicketFilter.class);
>> >
>> > @Singleton
>> > private static class CustomFilter extends WicketFilter {
>> > @Inject private Provider<WebApplication> webApplicationProvider;
>> >  @Override
>> > protected IWebApplicationFactory getApplicationFactory() {
>> > return new IWebApplicationFactory() {
>> > @Override
>> > public WebApplication createApplication(WicketFilter filter) {
>> > return webApplicationProvider.get();
>> > }
>> > @Override
>> > public void destroy(WicketFilter filter) {
>> > }
>> > };
>> > }
>> > }
>> >
>> > On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
>> > <[hidden email]>wrote:
>> >
>> >> Dan,
>> >>
>> >> I think you're right. Since in the WicketApplication init() function I
>> >> attempt to get the bootStrapInjector like this:
>> >>         Injector bootStrapInjector = (Injector)
>> >> this.getServletContext().getAttribute(Injector.class.getName());
>> >>
>> >> I just can't figure out how to get the injector into the
>> >> ServletContext before init() is run in my WicketApplication.
>> >>
>> >> Daniel
>> >>
>> >> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
>> wrote:
>> >> > Daniel,
>> >> >
>> >> > What you're doing should work, but I think you're giving
>> >> > your GuiceComponentInjector a null Injector. Unit tests don't go
>> through
>> >> > web.xml to set up its context listeners, so
>> >> > your GuiceServletContextListener never has a chance to construct and
>> >> > register an Injector with the ServletContext.
>> >> >
>> >> > Dan
>> >> >
>> >> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
>> >> [hidden email]>wrote:
>> >> >
>> >> >> Hi,
>> >> >>
>> >> >> I've integrated Guice into Wicket successfully, but I'm struggling
>> >> >> with the unittests. I'm not sure how to get the injector into my
>> >> >> HomePage class. Here's my setup.
>> >> >>
>> >> >> I'm using GuiceFilter with a GuiceServletContextListener. That
>> creates
>> >> >> the injector and a ServletModule which defines the WicketApplication.
>> >> >> I followed:
>> >> >> http://code.google.com/p/google-guice/wiki/ServletModule
>> >> >>
>> >> >> Here's some of MyGuiceServletConfig extends
>> GuiceServletContextListener
>> >> >>
>> >> >>     @Override
>> >> >>     protected Injector getInjector() {
>> >> >>         return Guice.createInjector(createServletModule(), new
>> >> >> MongoHoneybadgerModule());
>> >> >>     }
>> >> >>
>> >> >>     private ServletModule createServletModule() {
>> >> >>         return new ServletModule() {
>> >> >> ...
>> >> >>
>> >> >> In my WicketApplication extends WebApplication I have this init()
>> method
>> >> >>
>> >> >>     @Override
>> >> >>     public void init()
>> >> >>     {
>> >> >>         super.init();
>> >> >>         Injector bootStrapInjector = (Injector)
>> >> >> this.getServletContext().getAttribute(Injector.class.getName());
>> >> >>         getComponentInstantiationListeners().add(new
>> >> >> GuiceComponentInjector(this, bootStrapInjector));
>> >> >>     }
>> >> >>
>> >> >> Now, in my HomePage.java class I have
>> >> >>
>> >> >> public class HomePage extends WebPage {
>> >> >>     private static final long serialVersionUID = 1L;
>> >> >>     @Inject private Injector injector;
>> >> >>
>> >> >>     public HomePage(final PageParameters parameters) {
>> >> >>         super(parameters);
>> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>> >> >>
>> >> >>         add(new Label("version", myobj.getValue()));
>> >> >>     }
>> >> >> }
>> >> >>
>> >> >> This all runs great inside a web container as a servlet.
>> >> >>
>> >> >> The PROBLEM: I'm getting a NullPointerException on the line where I
>> >> >> reference the injector:
>> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>> >> >>
>> >> >> My test class is what was generated by the wicket quickstart. I'm not
>> >> >> sure how to make an injector available in setUp.
>> >> >>
>> >> >>     @Before
>> >> >>     public void setUp() {
>> >> >>         tester = new WicketTester(new WicketApplication());
>> >> >>     }
>> >> >>
>> >> >> Any ideas?
>> >> >>
>> >> >> Thanks,
>> >> >> Daniel
>> >> >>
>> >> >> ---------------------------------------------------------------------
>> >> >> To unsubscribe, e-mail: [hidden email]
>> >> >> For additional commands, e-mail: [hidden email]
>> >> >>
>> >> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: [hidden email]
>> >> For additional commands, e-mail: [hidden email]
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Dan Retzlaff
Yes, CustomFilter = CustomWicketFilter... Those aren't our actual names.
And yes, we provide filter parameters too. I just omitted them since they
weren't relevant to the Guice-based application instantiation I was
describing.

Do you have this in your web.xml?
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

On Fri, Oct 12, 2012 at 6:01 PM, Daniel Watrous <[hidden email]>wrote:

> Dan,
>
> Thanks. I've got unittests running now, but the WicketFilter doesn't
> seem to be processing. All I get when I run the applicaiton shows a
> jetty produced directory listing.
>
> In the snippet you provided before I think that CustomFilter and
> CustomeWicketFilter should be the same thing. Is that right?
>
> In my previous approach, when I bound the WicketFilter I included some
> parameters, like this:
>         filter("/*").through(WicketFilter.class,
> createWicketFilterInitParams());
>
> With this function
>
>     private Map<String, String> createWicketFilterInitParams() {
>         Map<String, String> wicketFilterParams = new HashMap<String,
> String>();
>         wicketFilterParams.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
>         wicketFilterParams.put("applicationClassName",
> "com.hp.honeybadger.web.WicketApplication");
>         return wicketFilterParams;
>     }
>
> I'm now trying to figure out how to make sure that the wicket filter
> is called...
>
> Daniel
>
> On Fri, Oct 12, 2012 at 11:03 AM, Dan Retzlaff <[hidden email]>
> wrote:
> > I follow you. WicketTester doesn't know about GuiceFilter, so you'll
> need a
> > different way of getting your Injector into your Wicket Application.
> Rather
> > than getting the Injector from your servlet context attributes, I'm
> > suggesting that you let Guice instantiate your Application so you can
> > @Inject the injector like any other dependency. The binding code I posted
> > previously does the (non-test) setup; for the test itself it's as simple
> as
> > https://gist.github.com/3880246.
> >
> > Hope that helps. By the way, I enjoyed your Wicket+EC2 article. Thanks
> for
> > that. :)
> >
> > On Fri, Oct 12, 2012 at 4:08 PM, Daniel Watrous <
> [hidden email]>wrote:
> >
> >> Dan,
> >>
> >> I'm not talking about my application. I'm talking about unittests.
> >> I've followed the Guice recommended way to integrate with servlets
> >> using the GuiceFilter.
> >>
> >> Now I'm trying to make the Wicket unittests work and I need the
> >> injector to be available in WicketTester.
> >>
> >> Daniel
> >>
> >> On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
> wrote:
> >> > For what it's worth, we instantiate our applications through Guice.
> >> Having
> >> > your application "go get" its Injector kind of violates the DI
> concept.
> >> >
> >> > filter("/*").through(WicketFilter.class);
> >> > bind(WebApplication.class).to(CustomWebApplication.class);
> >> > bind(WicketFilter.class).to(CustomWicketFilter.class);
> >> >
> >> > @Singleton
> >> > private static class CustomFilter extends WicketFilter {
> >> > @Inject private Provider<WebApplication> webApplicationProvider;
> >> >  @Override
> >> > protected IWebApplicationFactory getApplicationFactory() {
> >> > return new IWebApplicationFactory() {
> >> > @Override
> >> > public WebApplication createApplication(WicketFilter filter) {
> >> > return webApplicationProvider.get();
> >> > }
> >> > @Override
> >> > public void destroy(WicketFilter filter) {
> >> > }
> >> > };
> >> > }
> >> > }
> >> >
> >> > On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
> >> > <[hidden email]>wrote:
> >> >
> >> >> Dan,
> >> >>
> >> >> I think you're right. Since in the WicketApplication init() function
> I
> >> >> attempt to get the bootStrapInjector like this:
> >> >>         Injector bootStrapInjector = (Injector)
> >> >> this.getServletContext().getAttribute(Injector.class.getName());
> >> >>
> >> >> I just can't figure out how to get the injector into the
> >> >> ServletContext before init() is run in my WicketApplication.
> >> >>
> >> >> Daniel
> >> >>
> >> >> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
> >> wrote:
> >> >> > Daniel,
> >> >> >
> >> >> > What you're doing should work, but I think you're giving
> >> >> > your GuiceComponentInjector a null Injector. Unit tests don't go
> >> through
> >> >> > web.xml to set up its context listeners, so
> >> >> > your GuiceServletContextListener never has a chance to construct
> and
> >> >> > register an Injector with the ServletContext.
> >> >> >
> >> >> > Dan
> >> >> >
> >> >> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
> >> >> [hidden email]>wrote:
> >> >> >
> >> >> >> Hi,
> >> >> >>
> >> >> >> I've integrated Guice into Wicket successfully, but I'm struggling
> >> >> >> with the unittests. I'm not sure how to get the injector into my
> >> >> >> HomePage class. Here's my setup.
> >> >> >>
> >> >> >> I'm using GuiceFilter with a GuiceServletContextListener. That
> >> creates
> >> >> >> the injector and a ServletModule which defines the
> WicketApplication.
> >> >> >> I followed:
> >> >> >> http://code.google.com/p/google-guice/wiki/ServletModule
> >> >> >>
> >> >> >> Here's some of MyGuiceServletConfig extends
> >> GuiceServletContextListener
> >> >> >>
> >> >> >>     @Override
> >> >> >>     protected Injector getInjector() {
> >> >> >>         return Guice.createInjector(createServletModule(), new
> >> >> >> MongoHoneybadgerModule());
> >> >> >>     }
> >> >> >>
> >> >> >>     private ServletModule createServletModule() {
> >> >> >>         return new ServletModule() {
> >> >> >> ...
> >> >> >>
> >> >> >> In my WicketApplication extends WebApplication I have this init()
> >> method
> >> >> >>
> >> >> >>     @Override
> >> >> >>     public void init()
> >> >> >>     {
> >> >> >>         super.init();
> >> >> >>         Injector bootStrapInjector = (Injector)
> >> >> >> this.getServletContext().getAttribute(Injector.class.getName());
> >> >> >>         getComponentInstantiationListeners().add(new
> >> >> >> GuiceComponentInjector(this, bootStrapInjector));
> >> >> >>     }
> >> >> >>
> >> >> >> Now, in my HomePage.java class I have
> >> >> >>
> >> >> >> public class HomePage extends WebPage {
> >> >> >>     private static final long serialVersionUID = 1L;
> >> >> >>     @Inject private Injector injector;
> >> >> >>
> >> >> >>     public HomePage(final PageParameters parameters) {
> >> >> >>         super(parameters);
> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >> >> >>
> >> >> >>         add(new Label("version", myobj.getValue()));
> >> >> >>     }
> >> >> >> }
> >> >> >>
> >> >> >> This all runs great inside a web container as a servlet.
> >> >> >>
> >> >> >> The PROBLEM: I'm getting a NullPointerException on the line where
> I
> >> >> >> reference the injector:
> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >> >> >>
> >> >> >> My test class is what was generated by the wicket quickstart. I'm
> not
> >> >> >> sure how to make an injector available in setUp.
> >> >> >>
> >> >> >>     @Before
> >> >> >>     public void setUp() {
> >> >> >>         tester = new WicketTester(new WicketApplication());
> >> >> >>     }
> >> >> >>
> >> >> >> Any ideas?
> >> >> >>
> >> >> >> Thanks,
> >> >> >> Daniel
> >> >> >>
> >> >> >>
> ---------------------------------------------------------------------
> >> >> >> To unsubscribe, e-mail: [hidden email]
> >> >> >> For additional commands, e-mail: [hidden email]
> >> >> >>
> >> >> >>
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: [hidden email]
> >> >> For additional commands, e-mail: [hidden email]
> >> >>
> >> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Daniel Watrous-2
yes, that's what I have in my web.xml

On Fri, Oct 12, 2012 at 12:10 PM, Dan Retzlaff <[hidden email]> wrote:

> Yes, CustomFilter = CustomWicketFilter... Those aren't our actual names.
> And yes, we provide filter parameters too. I just omitted them since they
> weren't relevant to the Guice-based application instantiation I was
> describing.
>
> Do you have this in your web.xml?
> <filter>
> <filter-name>guiceFilter</filter-name>
> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
> </filter>
> <filter-mapping>
> <filter-name>guiceFilter</filter-name>
> <url-pattern>/*</url-pattern>
> </filter-mapping>
>
> On Fri, Oct 12, 2012 at 6:01 PM, Daniel Watrous <[hidden email]>wrote:
>
>> Dan,
>>
>> Thanks. I've got unittests running now, but the WicketFilter doesn't
>> seem to be processing. All I get when I run the applicaiton shows a
>> jetty produced directory listing.
>>
>> In the snippet you provided before I think that CustomFilter and
>> CustomeWicketFilter should be the same thing. Is that right?
>>
>> In my previous approach, when I bound the WicketFilter I included some
>> parameters, like this:
>>         filter("/*").through(WicketFilter.class,
>> createWicketFilterInitParams());
>>
>> With this function
>>
>>     private Map<String, String> createWicketFilterInitParams() {
>>         Map<String, String> wicketFilterParams = new HashMap<String,
>> String>();
>>         wicketFilterParams.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
>>         wicketFilterParams.put("applicationClassName",
>> "com.hp.honeybadger.web.WicketApplication");
>>         return wicketFilterParams;
>>     }
>>
>> I'm now trying to figure out how to make sure that the wicket filter
>> is called...
>>
>> Daniel
>>
>> On Fri, Oct 12, 2012 at 11:03 AM, Dan Retzlaff <[hidden email]>
>> wrote:
>> > I follow you. WicketTester doesn't know about GuiceFilter, so you'll
>> need a
>> > different way of getting your Injector into your Wicket Application.
>> Rather
>> > than getting the Injector from your servlet context attributes, I'm
>> > suggesting that you let Guice instantiate your Application so you can
>> > @Inject the injector like any other dependency. The binding code I posted
>> > previously does the (non-test) setup; for the test itself it's as simple
>> as
>> > https://gist.github.com/3880246.
>> >
>> > Hope that helps. By the way, I enjoyed your Wicket+EC2 article. Thanks
>> for
>> > that. :)
>> >
>> > On Fri, Oct 12, 2012 at 4:08 PM, Daniel Watrous <
>> [hidden email]>wrote:
>> >
>> >> Dan,
>> >>
>> >> I'm not talking about my application. I'm talking about unittests.
>> >> I've followed the Guice recommended way to integrate with servlets
>> >> using the GuiceFilter.
>> >>
>> >> Now I'm trying to make the Wicket unittests work and I need the
>> >> injector to be available in WicketTester.
>> >>
>> >> Daniel
>> >>
>> >> On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
>> wrote:
>> >> > For what it's worth, we instantiate our applications through Guice.
>> >> Having
>> >> > your application "go get" its Injector kind of violates the DI
>> concept.
>> >> >
>> >> > filter("/*").through(WicketFilter.class);
>> >> > bind(WebApplication.class).to(CustomWebApplication.class);
>> >> > bind(WicketFilter.class).to(CustomWicketFilter.class);
>> >> >
>> >> > @Singleton
>> >> > private static class CustomFilter extends WicketFilter {
>> >> > @Inject private Provider<WebApplication> webApplicationProvider;
>> >> >  @Override
>> >> > protected IWebApplicationFactory getApplicationFactory() {
>> >> > return new IWebApplicationFactory() {
>> >> > @Override
>> >> > public WebApplication createApplication(WicketFilter filter) {
>> >> > return webApplicationProvider.get();
>> >> > }
>> >> > @Override
>> >> > public void destroy(WicketFilter filter) {
>> >> > }
>> >> > };
>> >> > }
>> >> > }
>> >> >
>> >> > On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
>> >> > <[hidden email]>wrote:
>> >> >
>> >> >> Dan,
>> >> >>
>> >> >> I think you're right. Since in the WicketApplication init() function
>> I
>> >> >> attempt to get the bootStrapInjector like this:
>> >> >>         Injector bootStrapInjector = (Injector)
>> >> >> this.getServletContext().getAttribute(Injector.class.getName());
>> >> >>
>> >> >> I just can't figure out how to get the injector into the
>> >> >> ServletContext before init() is run in my WicketApplication.
>> >> >>
>> >> >> Daniel
>> >> >>
>> >> >> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
>> >> wrote:
>> >> >> > Daniel,
>> >> >> >
>> >> >> > What you're doing should work, but I think you're giving
>> >> >> > your GuiceComponentInjector a null Injector. Unit tests don't go
>> >> through
>> >> >> > web.xml to set up its context listeners, so
>> >> >> > your GuiceServletContextListener never has a chance to construct
>> and
>> >> >> > register an Injector with the ServletContext.
>> >> >> >
>> >> >> > Dan
>> >> >> >
>> >> >> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
>> >> >> [hidden email]>wrote:
>> >> >> >
>> >> >> >> Hi,
>> >> >> >>
>> >> >> >> I've integrated Guice into Wicket successfully, but I'm struggling
>> >> >> >> with the unittests. I'm not sure how to get the injector into my
>> >> >> >> HomePage class. Here's my setup.
>> >> >> >>
>> >> >> >> I'm using GuiceFilter with a GuiceServletContextListener. That
>> >> creates
>> >> >> >> the injector and a ServletModule which defines the
>> WicketApplication.
>> >> >> >> I followed:
>> >> >> >> http://code.google.com/p/google-guice/wiki/ServletModule
>> >> >> >>
>> >> >> >> Here's some of MyGuiceServletConfig extends
>> >> GuiceServletContextListener
>> >> >> >>
>> >> >> >>     @Override
>> >> >> >>     protected Injector getInjector() {
>> >> >> >>         return Guice.createInjector(createServletModule(), new
>> >> >> >> MongoHoneybadgerModule());
>> >> >> >>     }
>> >> >> >>
>> >> >> >>     private ServletModule createServletModule() {
>> >> >> >>         return new ServletModule() {
>> >> >> >> ...
>> >> >> >>
>> >> >> >> In my WicketApplication extends WebApplication I have this init()
>> >> method
>> >> >> >>
>> >> >> >>     @Override
>> >> >> >>     public void init()
>> >> >> >>     {
>> >> >> >>         super.init();
>> >> >> >>         Injector bootStrapInjector = (Injector)
>> >> >> >> this.getServletContext().getAttribute(Injector.class.getName());
>> >> >> >>         getComponentInstantiationListeners().add(new
>> >> >> >> GuiceComponentInjector(this, bootStrapInjector));
>> >> >> >>     }
>> >> >> >>
>> >> >> >> Now, in my HomePage.java class I have
>> >> >> >>
>> >> >> >> public class HomePage extends WebPage {
>> >> >> >>     private static final long serialVersionUID = 1L;
>> >> >> >>     @Inject private Injector injector;
>> >> >> >>
>> >> >> >>     public HomePage(final PageParameters parameters) {
>> >> >> >>         super(parameters);
>> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>> >> >> >>
>> >> >> >>         add(new Label("version", myobj.getValue()));
>> >> >> >>     }
>> >> >> >> }
>> >> >> >>
>> >> >> >> This all runs great inside a web container as a servlet.
>> >> >> >>
>> >> >> >> The PROBLEM: I'm getting a NullPointerException on the line where
>> I
>> >> >> >> reference the injector:
>> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>> >> >> >>
>> >> >> >> My test class is what was generated by the wicket quickstart. I'm
>> not
>> >> >> >> sure how to make an injector available in setUp.
>> >> >> >>
>> >> >> >>     @Before
>> >> >> >>     public void setUp() {
>> >> >> >>         tester = new WicketTester(new WicketApplication());
>> >> >> >>     }
>> >> >> >>
>> >> >> >> Any ideas?
>> >> >> >>
>> >> >> >> Thanks,
>> >> >> >> Daniel
>> >> >> >>
>> >> >> >>
>> ---------------------------------------------------------------------
>> >> >> >> To unsubscribe, e-mail: [hidden email]
>> >> >> >> For additional commands, e-mail: [hidden email]
>> >> >> >>
>> >> >> >>
>> >> >>
>> >> >> ---------------------------------------------------------------------
>> >> >> To unsubscribe, e-mail: [hidden email]
>> >> >> For additional commands, e-mail: [hidden email]
>> >> >>
>> >> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: [hidden email]
>> >> For additional commands, e-mail: [hidden email]
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Daniel Watrous-2
Dan,

Thanks for all your help. I finally worked through all the details and
have it working. Here's my write up:

http://software.danielwatrous.com/wicket-guice-including-unittests/

The last bit I needed clarification on was how to pass parameters to
the WicketFilter. I think the integration between Wicket and Guice is
clean and flexible.

Thanks again,
Daniel

On Fri, Oct 12, 2012 at 12:35 PM, Daniel Watrous
<[hidden email]> wrote:

> yes, that's what I have in my web.xml
>
> On Fri, Oct 12, 2012 at 12:10 PM, Dan Retzlaff <[hidden email]> wrote:
>> Yes, CustomFilter = CustomWicketFilter... Those aren't our actual names.
>> And yes, we provide filter parameters too. I just omitted them since they
>> weren't relevant to the Guice-based application instantiation I was
>> describing.
>>
>> Do you have this in your web.xml?
>> <filter>
>> <filter-name>guiceFilter</filter-name>
>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
>> </filter>
>> <filter-mapping>
>> <filter-name>guiceFilter</filter-name>
>> <url-pattern>/*</url-pattern>
>> </filter-mapping>
>>
>> On Fri, Oct 12, 2012 at 6:01 PM, Daniel Watrous <[hidden email]>wrote:
>>
>>> Dan,
>>>
>>> Thanks. I've got unittests running now, but the WicketFilter doesn't
>>> seem to be processing. All I get when I run the applicaiton shows a
>>> jetty produced directory listing.
>>>
>>> In the snippet you provided before I think that CustomFilter and
>>> CustomeWicketFilter should be the same thing. Is that right?
>>>
>>> In my previous approach, when I bound the WicketFilter I included some
>>> parameters, like this:
>>>         filter("/*").through(WicketFilter.class,
>>> createWicketFilterInitParams());
>>>
>>> With this function
>>>
>>>     private Map<String, String> createWicketFilterInitParams() {
>>>         Map<String, String> wicketFilterParams = new HashMap<String,
>>> String>();
>>>         wicketFilterParams.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
>>>         wicketFilterParams.put("applicationClassName",
>>> "com.hp.honeybadger.web.WicketApplication");
>>>         return wicketFilterParams;
>>>     }
>>>
>>> I'm now trying to figure out how to make sure that the wicket filter
>>> is called...
>>>
>>> Daniel
>>>
>>> On Fri, Oct 12, 2012 at 11:03 AM, Dan Retzlaff <[hidden email]>
>>> wrote:
>>> > I follow you. WicketTester doesn't know about GuiceFilter, so you'll
>>> need a
>>> > different way of getting your Injector into your Wicket Application.
>>> Rather
>>> > than getting the Injector from your servlet context attributes, I'm
>>> > suggesting that you let Guice instantiate your Application so you can
>>> > @Inject the injector like any other dependency. The binding code I posted
>>> > previously does the (non-test) setup; for the test itself it's as simple
>>> as
>>> > https://gist.github.com/3880246.
>>> >
>>> > Hope that helps. By the way, I enjoyed your Wicket+EC2 article. Thanks
>>> for
>>> > that. :)
>>> >
>>> > On Fri, Oct 12, 2012 at 4:08 PM, Daniel Watrous <
>>> [hidden email]>wrote:
>>> >
>>> >> Dan,
>>> >>
>>> >> I'm not talking about my application. I'm talking about unittests.
>>> >> I've followed the Guice recommended way to integrate with servlets
>>> >> using the GuiceFilter.
>>> >>
>>> >> Now I'm trying to make the Wicket unittests work and I need the
>>> >> injector to be available in WicketTester.
>>> >>
>>> >> Daniel
>>> >>
>>> >> On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
>>> wrote:
>>> >> > For what it's worth, we instantiate our applications through Guice.
>>> >> Having
>>> >> > your application "go get" its Injector kind of violates the DI
>>> concept.
>>> >> >
>>> >> > filter("/*").through(WicketFilter.class);
>>> >> > bind(WebApplication.class).to(CustomWebApplication.class);
>>> >> > bind(WicketFilter.class).to(CustomWicketFilter.class);
>>> >> >
>>> >> > @Singleton
>>> >> > private static class CustomFilter extends WicketFilter {
>>> >> > @Inject private Provider<WebApplication> webApplicationProvider;
>>> >> >  @Override
>>> >> > protected IWebApplicationFactory getApplicationFactory() {
>>> >> > return new IWebApplicationFactory() {
>>> >> > @Override
>>> >> > public WebApplication createApplication(WicketFilter filter) {
>>> >> > return webApplicationProvider.get();
>>> >> > }
>>> >> > @Override
>>> >> > public void destroy(WicketFilter filter) {
>>> >> > }
>>> >> > };
>>> >> > }
>>> >> > }
>>> >> >
>>> >> > On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
>>> >> > <[hidden email]>wrote:
>>> >> >
>>> >> >> Dan,
>>> >> >>
>>> >> >> I think you're right. Since in the WicketApplication init() function
>>> I
>>> >> >> attempt to get the bootStrapInjector like this:
>>> >> >>         Injector bootStrapInjector = (Injector)
>>> >> >> this.getServletContext().getAttribute(Injector.class.getName());
>>> >> >>
>>> >> >> I just can't figure out how to get the injector into the
>>> >> >> ServletContext before init() is run in my WicketApplication.
>>> >> >>
>>> >> >> Daniel
>>> >> >>
>>> >> >> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
>>> >> wrote:
>>> >> >> > Daniel,
>>> >> >> >
>>> >> >> > What you're doing should work, but I think you're giving
>>> >> >> > your GuiceComponentInjector a null Injector. Unit tests don't go
>>> >> through
>>> >> >> > web.xml to set up its context listeners, so
>>> >> >> > your GuiceServletContextListener never has a chance to construct
>>> and
>>> >> >> > register an Injector with the ServletContext.
>>> >> >> >
>>> >> >> > Dan
>>> >> >> >
>>> >> >> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
>>> >> >> [hidden email]>wrote:
>>> >> >> >
>>> >> >> >> Hi,
>>> >> >> >>
>>> >> >> >> I've integrated Guice into Wicket successfully, but I'm struggling
>>> >> >> >> with the unittests. I'm not sure how to get the injector into my
>>> >> >> >> HomePage class. Here's my setup.
>>> >> >> >>
>>> >> >> >> I'm using GuiceFilter with a GuiceServletContextListener. That
>>> >> creates
>>> >> >> >> the injector and a ServletModule which defines the
>>> WicketApplication.
>>> >> >> >> I followed:
>>> >> >> >> http://code.google.com/p/google-guice/wiki/ServletModule
>>> >> >> >>
>>> >> >> >> Here's some of MyGuiceServletConfig extends
>>> >> GuiceServletContextListener
>>> >> >> >>
>>> >> >> >>     @Override
>>> >> >> >>     protected Injector getInjector() {
>>> >> >> >>         return Guice.createInjector(createServletModule(), new
>>> >> >> >> MongoHoneybadgerModule());
>>> >> >> >>     }
>>> >> >> >>
>>> >> >> >>     private ServletModule createServletModule() {
>>> >> >> >>         return new ServletModule() {
>>> >> >> >> ...
>>> >> >> >>
>>> >> >> >> In my WicketApplication extends WebApplication I have this init()
>>> >> method
>>> >> >> >>
>>> >> >> >>     @Override
>>> >> >> >>     public void init()
>>> >> >> >>     {
>>> >> >> >>         super.init();
>>> >> >> >>         Injector bootStrapInjector = (Injector)
>>> >> >> >> this.getServletContext().getAttribute(Injector.class.getName());
>>> >> >> >>         getComponentInstantiationListeners().add(new
>>> >> >> >> GuiceComponentInjector(this, bootStrapInjector));
>>> >> >> >>     }
>>> >> >> >>
>>> >> >> >> Now, in my HomePage.java class I have
>>> >> >> >>
>>> >> >> >> public class HomePage extends WebPage {
>>> >> >> >>     private static final long serialVersionUID = 1L;
>>> >> >> >>     @Inject private Injector injector;
>>> >> >> >>
>>> >> >> >>     public HomePage(final PageParameters parameters) {
>>> >> >> >>         super(parameters);
>>> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>>> >> >> >>
>>> >> >> >>         add(new Label("version", myobj.getValue()));
>>> >> >> >>     }
>>> >> >> >> }
>>> >> >> >>
>>> >> >> >> This all runs great inside a web container as a servlet.
>>> >> >> >>
>>> >> >> >> The PROBLEM: I'm getting a NullPointerException on the line where
>>> I
>>> >> >> >> reference the injector:
>>> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>>> >> >> >>
>>> >> >> >> My test class is what was generated by the wicket quickstart. I'm
>>> not
>>> >> >> >> sure how to make an injector available in setUp.
>>> >> >> >>
>>> >> >> >>     @Before
>>> >> >> >>     public void setUp() {
>>> >> >> >>         tester = new WicketTester(new WicketApplication());
>>> >> >> >>     }
>>> >> >> >>
>>> >> >> >> Any ideas?
>>> >> >> >>
>>> >> >> >> Thanks,
>>> >> >> >> Daniel
>>> >> >> >>
>>> >> >> >>
>>> ---------------------------------------------------------------------
>>> >> >> >> To unsubscribe, e-mail: [hidden email]
>>> >> >> >> For additional commands, e-mail: [hidden email]
>>> >> >> >>
>>> >> >> >>
>>> >> >>
>>> >> >> ---------------------------------------------------------------------
>>> >> >> To unsubscribe, e-mail: [hidden email]
>>> >> >> For additional commands, e-mail: [hidden email]
>>> >> >>
>>> >> >>
>>> >>
>>> >> ---------------------------------------------------------------------
>>> >> To unsubscribe, e-mail: [hidden email]
>>> >> For additional commands, e-mail: [hidden email]
>>> >>
>>> >>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Martin Grigorov-4
Hi Daniel,

public class HomePage extends WebPage {
    private static final long serialVersionUID = 1L;
    @Inject private Injector injector;

    public HomePage(final PageParameters parameters) {
        super(parameters);

        add(new Label("version",
getApplication().getFrameworkSettings().getVersion()));

        // TODO Add your page's components here
        QuickLink quickLink = injector.getInstance(QuickLink.class);
        add(new Label("quickLink ", quickLink.buildQuickLink()));
    }
}

Why do you inject the Injector and then ask it for beans instead of
injecting the bean directly and use it ?

On Mon, Oct 15, 2012 at 6:51 PM, Daniel Watrous
<[hidden email]> wrote:

> Dan,
>
> Thanks for all your help. I finally worked through all the details and
> have it working. Here's my write up:
>
> http://software.danielwatrous.com/wicket-guice-including-unittests/
>
> The last bit I needed clarification on was how to pass parameters to
> the WicketFilter. I think the integration between Wicket and Guice is
> clean and flexible.
>
> Thanks again,
> Daniel
>
> On Fri, Oct 12, 2012 at 12:35 PM, Daniel Watrous
> <[hidden email]> wrote:
>> yes, that's what I have in my web.xml
>>
>> On Fri, Oct 12, 2012 at 12:10 PM, Dan Retzlaff <[hidden email]> wrote:
>>> Yes, CustomFilter = CustomWicketFilter... Those aren't our actual names.
>>> And yes, we provide filter parameters too. I just omitted them since they
>>> weren't relevant to the Guice-based application instantiation I was
>>> describing.
>>>
>>> Do you have this in your web.xml?
>>> <filter>
>>> <filter-name>guiceFilter</filter-name>
>>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
>>> </filter>
>>> <filter-mapping>
>>> <filter-name>guiceFilter</filter-name>
>>> <url-pattern>/*</url-pattern>
>>> </filter-mapping>
>>>
>>> On Fri, Oct 12, 2012 at 6:01 PM, Daniel Watrous <[hidden email]>wrote:
>>>
>>>> Dan,
>>>>
>>>> Thanks. I've got unittests running now, but the WicketFilter doesn't
>>>> seem to be processing. All I get when I run the applicaiton shows a
>>>> jetty produced directory listing.
>>>>
>>>> In the snippet you provided before I think that CustomFilter and
>>>> CustomeWicketFilter should be the same thing. Is that right?
>>>>
>>>> In my previous approach, when I bound the WicketFilter I included some
>>>> parameters, like this:
>>>>         filter("/*").through(WicketFilter.class,
>>>> createWicketFilterInitParams());
>>>>
>>>> With this function
>>>>
>>>>     private Map<String, String> createWicketFilterInitParams() {
>>>>         Map<String, String> wicketFilterParams = new HashMap<String,
>>>> String>();
>>>>         wicketFilterParams.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
>>>>         wicketFilterParams.put("applicationClassName",
>>>> "com.hp.honeybadger.web.WicketApplication");
>>>>         return wicketFilterParams;
>>>>     }
>>>>
>>>> I'm now trying to figure out how to make sure that the wicket filter
>>>> is called...
>>>>
>>>> Daniel
>>>>
>>>> On Fri, Oct 12, 2012 at 11:03 AM, Dan Retzlaff <[hidden email]>
>>>> wrote:
>>>> > I follow you. WicketTester doesn't know about GuiceFilter, so you'll
>>>> need a
>>>> > different way of getting your Injector into your Wicket Application.
>>>> Rather
>>>> > than getting the Injector from your servlet context attributes, I'm
>>>> > suggesting that you let Guice instantiate your Application so you can
>>>> > @Inject the injector like any other dependency. The binding code I posted
>>>> > previously does the (non-test) setup; for the test itself it's as simple
>>>> as
>>>> > https://gist.github.com/3880246.
>>>> >
>>>> > Hope that helps. By the way, I enjoyed your Wicket+EC2 article. Thanks
>>>> for
>>>> > that. :)
>>>> >
>>>> > On Fri, Oct 12, 2012 at 4:08 PM, Daniel Watrous <
>>>> [hidden email]>wrote:
>>>> >
>>>> >> Dan,
>>>> >>
>>>> >> I'm not talking about my application. I'm talking about unittests.
>>>> >> I've followed the Guice recommended way to integrate with servlets
>>>> >> using the GuiceFilter.
>>>> >>
>>>> >> Now I'm trying to make the Wicket unittests work and I need the
>>>> >> injector to be available in WicketTester.
>>>> >>
>>>> >> Daniel
>>>> >>
>>>> >> On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
>>>> wrote:
>>>> >> > For what it's worth, we instantiate our applications through Guice.
>>>> >> Having
>>>> >> > your application "go get" its Injector kind of violates the DI
>>>> concept.
>>>> >> >
>>>> >> > filter("/*").through(WicketFilter.class);
>>>> >> > bind(WebApplication.class).to(CustomWebApplication.class);
>>>> >> > bind(WicketFilter.class).to(CustomWicketFilter.class);
>>>> >> >
>>>> >> > @Singleton
>>>> >> > private static class CustomFilter extends WicketFilter {
>>>> >> > @Inject private Provider<WebApplication> webApplicationProvider;
>>>> >> >  @Override
>>>> >> > protected IWebApplicationFactory getApplicationFactory() {
>>>> >> > return new IWebApplicationFactory() {
>>>> >> > @Override
>>>> >> > public WebApplication createApplication(WicketFilter filter) {
>>>> >> > return webApplicationProvider.get();
>>>> >> > }
>>>> >> > @Override
>>>> >> > public void destroy(WicketFilter filter) {
>>>> >> > }
>>>> >> > };
>>>> >> > }
>>>> >> > }
>>>> >> >
>>>> >> > On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
>>>> >> > <[hidden email]>wrote:
>>>> >> >
>>>> >> >> Dan,
>>>> >> >>
>>>> >> >> I think you're right. Since in the WicketApplication init() function
>>>> I
>>>> >> >> attempt to get the bootStrapInjector like this:
>>>> >> >>         Injector bootStrapInjector = (Injector)
>>>> >> >> this.getServletContext().getAttribute(Injector.class.getName());
>>>> >> >>
>>>> >> >> I just can't figure out how to get the injector into the
>>>> >> >> ServletContext before init() is run in my WicketApplication.
>>>> >> >>
>>>> >> >> Daniel
>>>> >> >>
>>>> >> >> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]>
>>>> >> wrote:
>>>> >> >> > Daniel,
>>>> >> >> >
>>>> >> >> > What you're doing should work, but I think you're giving
>>>> >> >> > your GuiceComponentInjector a null Injector. Unit tests don't go
>>>> >> through
>>>> >> >> > web.xml to set up its context listeners, so
>>>> >> >> > your GuiceServletContextListener never has a chance to construct
>>>> and
>>>> >> >> > register an Injector with the ServletContext.
>>>> >> >> >
>>>> >> >> > Dan
>>>> >> >> >
>>>> >> >> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
>>>> >> >> [hidden email]>wrote:
>>>> >> >> >
>>>> >> >> >> Hi,
>>>> >> >> >>
>>>> >> >> >> I've integrated Guice into Wicket successfully, but I'm struggling
>>>> >> >> >> with the unittests. I'm not sure how to get the injector into my
>>>> >> >> >> HomePage class. Here's my setup.
>>>> >> >> >>
>>>> >> >> >> I'm using GuiceFilter with a GuiceServletContextListener. That
>>>> >> creates
>>>> >> >> >> the injector and a ServletModule which defines the
>>>> WicketApplication.
>>>> >> >> >> I followed:
>>>> >> >> >> http://code.google.com/p/google-guice/wiki/ServletModule
>>>> >> >> >>
>>>> >> >> >> Here's some of MyGuiceServletConfig extends
>>>> >> GuiceServletContextListener
>>>> >> >> >>
>>>> >> >> >>     @Override
>>>> >> >> >>     protected Injector getInjector() {
>>>> >> >> >>         return Guice.createInjector(createServletModule(), new
>>>> >> >> >> MongoHoneybadgerModule());
>>>> >> >> >>     }
>>>> >> >> >>
>>>> >> >> >>     private ServletModule createServletModule() {
>>>> >> >> >>         return new ServletModule() {
>>>> >> >> >> ...
>>>> >> >> >>
>>>> >> >> >> In my WicketApplication extends WebApplication I have this init()
>>>> >> method
>>>> >> >> >>
>>>> >> >> >>     @Override
>>>> >> >> >>     public void init()
>>>> >> >> >>     {
>>>> >> >> >>         super.init();
>>>> >> >> >>         Injector bootStrapInjector = (Injector)
>>>> >> >> >> this.getServletContext().getAttribute(Injector.class.getName());
>>>> >> >> >>         getComponentInstantiationListeners().add(new
>>>> >> >> >> GuiceComponentInjector(this, bootStrapInjector));
>>>> >> >> >>     }
>>>> >> >> >>
>>>> >> >> >> Now, in my HomePage.java class I have
>>>> >> >> >>
>>>> >> >> >> public class HomePage extends WebPage {
>>>> >> >> >>     private static final long serialVersionUID = 1L;
>>>> >> >> >>     @Inject private Injector injector;
>>>> >> >> >>
>>>> >> >> >>     public HomePage(final PageParameters parameters) {
>>>> >> >> >>         super(parameters);
>>>> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>>>> >> >> >>
>>>> >> >> >>         add(new Label("version", myobj.getValue()));
>>>> >> >> >>     }
>>>> >> >> >> }
>>>> >> >> >>
>>>> >> >> >> This all runs great inside a web container as a servlet.
>>>> >> >> >>
>>>> >> >> >> The PROBLEM: I'm getting a NullPointerException on the line where
>>>> I
>>>> >> >> >> reference the injector:
>>>> >> >> >>         SomeType myobj = injector.getInstance(SomeType.class);
>>>> >> >> >>
>>>> >> >> >> My test class is what was generated by the wicket quickstart. I'm
>>>> not
>>>> >> >> >> sure how to make an injector available in setUp.
>>>> >> >> >>
>>>> >> >> >>     @Before
>>>> >> >> >>     public void setUp() {
>>>> >> >> >>         tester = new WicketTester(new WicketApplication());
>>>> >> >> >>     }
>>>> >> >> >>
>>>> >> >> >> Any ideas?
>>>> >> >> >>
>>>> >> >> >> Thanks,
>>>> >> >> >> Daniel
>>>> >> >> >>
>>>> >> >> >>
>>>> ---------------------------------------------------------------------
>>>> >> >> >> To unsubscribe, e-mail: [hidden email]
>>>> >> >> >> For additional commands, e-mail: [hidden email]
>>>> >> >> >>
>>>> >> >> >>
>>>> >> >>
>>>> >> >> ---------------------------------------------------------------------
>>>> >> >> To unsubscribe, e-mail: [hidden email]
>>>> >> >> For additional commands, e-mail: [hidden email]
>>>> >> >>
>>>> >> >>
>>>> >>
>>>> >> ---------------------------------------------------------------------
>>>> >> To unsubscribe, e-mail: [hidden email]
>>>> >> For additional commands, e-mail: [hidden email]
>>>> >>
>>>> >>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>



--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Wicket + Guice + unittests

Daniel Watrous-2
Martin,

In this case it would be just as easy to inject a specific bean.

I did it this way because it matched some other examples that I saw.

Daniel
On Oct 16, 2012 1:08 AM, "Martin Grigorov" <[hidden email]> wrote:

> Hi Daniel,
>
> public class HomePage extends WebPage {
>     private static final long serialVersionUID = 1L;
>     @Inject private Injector injector;
>
>     public HomePage(final PageParameters parameters) {
>         super(parameters);
>
>         add(new Label("version",
> getApplication().getFrameworkSettings().getVersion()));
>
>         // TODO Add your page's components here
>         QuickLink quickLink = injector.getInstance(QuickLink.class);
>         add(new Label("quickLink ", quickLink.buildQuickLink()));
>     }
> }
>
> Why do you inject the Injector and then ask it for beans instead of
> injecting the bean directly and use it ?
>
> On Mon, Oct 15, 2012 at 6:51 PM, Daniel Watrous
> <[hidden email]> wrote:
> > Dan,
> >
> > Thanks for all your help. I finally worked through all the details and
> > have it working. Here's my write up:
> >
> > http://software.danielwatrous.com/wicket-guice-including-unittests/
> >
> > The last bit I needed clarification on was how to pass parameters to
> > the WicketFilter. I think the integration between Wicket and Guice is
> > clean and flexible.
> >
> > Thanks again,
> > Daniel
> >
> > On Fri, Oct 12, 2012 at 12:35 PM, Daniel Watrous
> > <[hidden email]> wrote:
> >> yes, that's what I have in my web.xml
> >>
> >> On Fri, Oct 12, 2012 at 12:10 PM, Dan Retzlaff <[hidden email]>
> wrote:
> >>> Yes, CustomFilter = CustomWicketFilter... Those aren't our actual
> names.
> >>> And yes, we provide filter parameters too. I just omitted them since
> they
> >>> weren't relevant to the Guice-based application instantiation I was
> >>> describing.
> >>>
> >>> Do you have this in your web.xml?
> >>> <filter>
> >>> <filter-name>guiceFilter</filter-name>
> >>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
> >>> </filter>
> >>> <filter-mapping>
> >>> <filter-name>guiceFilter</filter-name>
> >>> <url-pattern>/*</url-pattern>
> >>> </filter-mapping>
> >>>
> >>> On Fri, Oct 12, 2012 at 6:01 PM, Daniel Watrous <
> [hidden email]>wrote:
> >>>
> >>>> Dan,
> >>>>
> >>>> Thanks. I've got unittests running now, but the WicketFilter doesn't
> >>>> seem to be processing. All I get when I run the applicaiton shows a
> >>>> jetty produced directory listing.
> >>>>
> >>>> In the snippet you provided before I think that CustomFilter and
> >>>> CustomeWicketFilter should be the same thing. Is that right?
> >>>>
> >>>> In my previous approach, when I bound the WicketFilter I included some
> >>>> parameters, like this:
> >>>>         filter("/*").through(WicketFilter.class,
> >>>> createWicketFilterInitParams());
> >>>>
> >>>> With this function
> >>>>
> >>>>     private Map<String, String> createWicketFilterInitParams() {
> >>>>         Map<String, String> wicketFilterParams = new HashMap<String,
> >>>> String>();
> >>>>         wicketFilterParams.put(WicketFilter.FILTER_MAPPING_PARAM,
> "/*");
> >>>>         wicketFilterParams.put("applicationClassName",
> >>>> "com.hp.honeybadger.web.WicketApplication");
> >>>>         return wicketFilterParams;
> >>>>     }
> >>>>
> >>>> I'm now trying to figure out how to make sure that the wicket filter
> >>>> is called...
> >>>>
> >>>> Daniel
> >>>>
> >>>> On Fri, Oct 12, 2012 at 11:03 AM, Dan Retzlaff <[hidden email]>
> >>>> wrote:
> >>>> > I follow you. WicketTester doesn't know about GuiceFilter, so you'll
> >>>> need a
> >>>> > different way of getting your Injector into your Wicket Application.
> >>>> Rather
> >>>> > than getting the Injector from your servlet context attributes, I'm
> >>>> > suggesting that you let Guice instantiate your Application so you
> can
> >>>> > @Inject the injector like any other dependency. The binding code I
> posted
> >>>> > previously does the (non-test) setup; for the test itself it's as
> simple
> >>>> as
> >>>> > https://gist.github.com/3880246.
> >>>> >
> >>>> > Hope that helps. By the way, I enjoyed your Wicket+EC2 article.
> Thanks
> >>>> for
> >>>> > that. :)
> >>>> >
> >>>> > On Fri, Oct 12, 2012 at 4:08 PM, Daniel Watrous <
> >>>> [hidden email]>wrote:
> >>>> >
> >>>> >> Dan,
> >>>> >>
> >>>> >> I'm not talking about my application. I'm talking about unittests.
> >>>> >> I've followed the Guice recommended way to integrate with servlets
> >>>> >> using the GuiceFilter.
> >>>> >>
> >>>> >> Now I'm trying to make the Wicket unittests work and I need the
> >>>> >> injector to be available in WicketTester.
> >>>> >>
> >>>> >> Daniel
> >>>> >>
> >>>> >> On Thu, Oct 11, 2012 at 6:10 PM, Dan Retzlaff <[hidden email]
> >
> >>>> wrote:
> >>>> >> > For what it's worth, we instantiate our applications through
> Guice.
> >>>> >> Having
> >>>> >> > your application "go get" its Injector kind of violates the DI
> >>>> concept.
> >>>> >> >
> >>>> >> > filter("/*").through(WicketFilter.class);
> >>>> >> > bind(WebApplication.class).to(CustomWebApplication.class);
> >>>> >> > bind(WicketFilter.class).to(CustomWicketFilter.class);
> >>>> >> >
> >>>> >> > @Singleton
> >>>> >> > private static class CustomFilter extends WicketFilter {
> >>>> >> > @Inject private Provider<WebApplication> webApplicationProvider;
> >>>> >> >  @Override
> >>>> >> > protected IWebApplicationFactory getApplicationFactory() {
> >>>> >> > return new IWebApplicationFactory() {
> >>>> >> > @Override
> >>>> >> > public WebApplication createApplication(WicketFilter filter) {
> >>>> >> > return webApplicationProvider.get();
> >>>> >> > }
> >>>> >> > @Override
> >>>> >> > public void destroy(WicketFilter filter) {
> >>>> >> > }
> >>>> >> > };
> >>>> >> > }
> >>>> >> > }
> >>>> >> >
> >>>> >> > On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
> >>>> >> > <[hidden email]>wrote:
> >>>> >> >
> >>>> >> >> Dan,
> >>>> >> >>
> >>>> >> >> I think you're right. Since in the WicketApplication init()
> function
> >>>> I
> >>>> >> >> attempt to get the bootStrapInjector like this:
> >>>> >> >>         Injector bootStrapInjector = (Injector)
> >>>> >> >> this.getServletContext().getAttribute(Injector.class.getName());
> >>>> >> >>
> >>>> >> >> I just can't figure out how to get the injector into the
> >>>> >> >> ServletContext before init() is run in my WicketApplication.
> >>>> >> >>
> >>>> >> >> Daniel
> >>>> >> >>
> >>>> >> >> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <
> [hidden email]>
> >>>> >> wrote:
> >>>> >> >> > Daniel,
> >>>> >> >> >
> >>>> >> >> > What you're doing should work, but I think you're giving
> >>>> >> >> > your GuiceComponentInjector a null Injector. Unit tests don't
> go
> >>>> >> through
> >>>> >> >> > web.xml to set up its context listeners, so
> >>>> >> >> > your GuiceServletContextListener never has a chance to
> construct
> >>>> and
> >>>> >> >> > register an Injector with the ServletContext.
> >>>> >> >> >
> >>>> >> >> > Dan
> >>>> >> >> >
> >>>> >> >> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
> >>>> >> >> [hidden email]>wrote:
> >>>> >> >> >
> >>>> >> >> >> Hi,
> >>>> >> >> >>
> >>>> >> >> >> I've integrated Guice into Wicket successfully, but I'm
> struggling
> >>>> >> >> >> with the unittests. I'm not sure how to get the injector
> into my
> >>>> >> >> >> HomePage class. Here's my setup.
> >>>> >> >> >>
> >>>> >> >> >> I'm using GuiceFilter with a GuiceServletContextListener.
> That
> >>>> >> creates
> >>>> >> >> >> the injector and a ServletModule which defines the
> >>>> WicketApplication.
> >>>> >> >> >> I followed:
> >>>> >> >> >> http://code.google.com/p/google-guice/wiki/ServletModule
> >>>> >> >> >>
> >>>> >> >> >> Here's some of MyGuiceServletConfig extends
> >>>> >> GuiceServletContextListener
> >>>> >> >> >>
> >>>> >> >> >>     @Override
> >>>> >> >> >>     protected Injector getInjector() {
> >>>> >> >> >>         return Guice.createInjector(createServletModule(),
> new
> >>>> >> >> >> MongoHoneybadgerModule());
> >>>> >> >> >>     }
> >>>> >> >> >>
> >>>> >> >> >>     private ServletModule createServletModule() {
> >>>> >> >> >>         return new ServletModule() {
> >>>> >> >> >> ...
> >>>> >> >> >>
> >>>> >> >> >> In my WicketApplication extends WebApplication I have this
> init()
> >>>> >> method
> >>>> >> >> >>
> >>>> >> >> >>     @Override
> >>>> >> >> >>     public void init()
> >>>> >> >> >>     {
> >>>> >> >> >>         super.init();
> >>>> >> >> >>         Injector bootStrapInjector = (Injector)
> >>>> >> >> >>
> this.getServletContext().getAttribute(Injector.class.getName());
> >>>> >> >> >>         getComponentInstantiationListeners().add(new
> >>>> >> >> >> GuiceComponentInjector(this, bootStrapInjector));
> >>>> >> >> >>     }
> >>>> >> >> >>
> >>>> >> >> >> Now, in my HomePage.java class I have
> >>>> >> >> >>
> >>>> >> >> >> public class HomePage extends WebPage {
> >>>> >> >> >>     private static final long serialVersionUID = 1L;
> >>>> >> >> >>     @Inject private Injector injector;
> >>>> >> >> >>
> >>>> >> >> >>     public HomePage(final PageParameters parameters) {
> >>>> >> >> >>         super(parameters);
> >>>> >> >> >>         SomeType myobj =
> injector.getInstance(SomeType.class);
> >>>> >> >> >>
> >>>> >> >> >>         add(new Label("version", myobj.getValue()));
> >>>> >> >> >>     }
> >>>> >> >> >> }
> >>>> >> >> >>
> >>>> >> >> >> This all runs great inside a web container as a servlet.
> >>>> >> >> >>
> >>>> >> >> >> The PROBLEM: I'm getting a NullPointerException on the line
> where
> >>>> I
> >>>> >> >> >> reference the injector:
> >>>> >> >> >>         SomeType myobj =
> injector.getInstance(SomeType.class);
> >>>> >> >> >>
> >>>> >> >> >> My test class is what was generated by the wicket
> quickstart. I'm
> >>>> not
> >>>> >> >> >> sure how to make an injector available in setUp.
> >>>> >> >> >>
> >>>> >> >> >>     @Before
> >>>> >> >> >>     public void setUp() {
> >>>> >> >> >>         tester = new WicketTester(new WicketApplication());
> >>>> >> >> >>     }
> >>>> >> >> >>
> >>>> >> >> >> Any ideas?
> >>>> >> >> >>
> >>>> >> >> >> Thanks,
> >>>> >> >> >> Daniel
> >>>> >> >> >>
> >>>> >> >> >>
> >>>> ---------------------------------------------------------------------
> >>>> >> >> >> To unsubscribe, e-mail: [hidden email]
> >>>> >> >> >> For additional commands, e-mail:
> [hidden email]
> >>>> >> >> >>
> >>>> >> >> >>
> >>>> >> >>
> >>>> >> >>
> ---------------------------------------------------------------------
> >>>> >> >> To unsubscribe, e-mail: [hidden email]
> >>>> >> >> For additional commands, e-mail: [hidden email]
> >>>> >> >>
> >>>> >> >>
> >>>> >>
> >>>> >>
> ---------------------------------------------------------------------
> >>>> >> To unsubscribe, e-mail: [hidden email]
> >>>> >> For additional commands, e-mail: [hidden email]
> >>>> >>
> >>>> >>
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: [hidden email]
> >>>> For additional commands, e-mail: [hidden email]
> >>>>
> >>>>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>