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 :

    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 :) .


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

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:

Here's some of MyGuiceServletConfig extends GuiceServletContextListener

     protected Injector getInjector() {
         return Guice.createInjector(createServletModule(), new

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

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

     public void init()
         Injector bootStrapInjector = (Injector)
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) {
         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.

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

Any ideas?


To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to