Hi,

You can do something like:

class MyPage extends WebPage {

@Inject private DAO dao;

@Inject @Nullable private UserBean currentUser;

... // use dao or currentUser anywhere in your class

}

And Guice will try to inject these beans if they are defined in a Guice
module.
See https://code.google.com/p/google-guice/wiki/UseNullable


On Tue, Jan 29, 2013 at 2:27 AM, Bill Speirs <bill.spe...@gmail.com> wrote:

> Still struggling with how to do this. Martin, I understand that most people
> will simply call new PanelA and new PanelB inside their MyPage code instead
> of trying to inject them. However, how does one setup a page that requires
> some service (DAO for example) and that might optionally require a
> UserBean.
>
> So without using DI, I'd simply have 2 constructors:
>
> MyPage(DAO myDao)
>
> MyPage(DAO myDao, UserBean currentUser)
>
> But I cannot create that second constructor as I don't have anything to
> bind (using Guice terms) UserBean to.
>
> Am I required to make a factory for MyPage at that point? Is it bad/wrong
> to pass the UserBean into the constructor via a response page:
>
> setResponsePage(new MyPage(new DAO(), currentUser));
>
> Thanks...
>
> Bill-
>
>
> On Fri, Nov 30, 2012 at 3:35 AM, Martin Grigorov <mgrigo...@apache.org
> >wrote:
>
> > Hi,
> >
> > Most of the time people inject services to their components.
> >
> > To test just components you can use WicketTester#startComponentInPage()
> > methods.
> >
> >
> > On Fri, Nov 30, 2012 at 3:18 AM, William Speirs <wspe...@apache.org>
> > wrote:
> >
> > > I'm having trouble understanding how to inject components into a page
> so
> > > that the page will be easy to unit test later. Say I have a page that
> > > contains two panels. I can easily use constructor injection to inject
> > these
> > > panels into the page:
> > >
> > > class MyPage extends WebPage {
> > > @Inject
> > > public MyPage(PanelA a, PanelB b) { ... }
> > > }
> > >
> > > The problem is that all Panels require an id during construction.[1]
> How
> > do
> > > I supply the id to my Panels? I could simply construct every PanelA
> with
> > an
> > > id of "panela" and every PanelB with an id of "panel", but that doesn't
> > > seem very flexible. What do other people do in this situation? The hope
> > > would be to pass mocked panels into the page during unit testing,
> > > the separately test each panel.
> > >
> > > What if instead of a panel it was a button where the onSubmit method
> must
> > > be specified by overriding the method. How does one go about injecting
> > such
> > > a component so that it's still easy to test later in unit tests?
> > >
> > > All thoughts and/or best practices are greatly welcomed. For reference
> > I'm
> > > using Guice as my dependency injection framework and
> > > GuiceWebApplicationFactory to inject components into pages.
> > >
> > > Thanks...
> > >
> > > Bill-
> > >
> > > [1]
> > >
> > >
> >
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/markup/html/panel/Panel.html
> > >
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com <http://jweekend.com/>
> >
>



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

Reply via email to