1

I've started playing with Wicket and I've chosen Guice as dependency injection framework. Now I'm trying to learn how to write a unit test for a WebPage object.

I googled a bit and I've found this post but it mentioned AtUnit so I decided to give it a try.

My WebPage class looks like this

public class MyWebPage extends WebPage
{
    @Inject MyService service;
    public MyWebPage()
    {
        //here I build my components and use injected object.
        service.get(id);
        ....    
    }
}

I created mock to replace any production MyServiceImpl with it and I guess that Guice in hand with AtUnit should inject it.

Now the problems are:

  1. AtUnit expects that I mark target object with @Unit - that is all right as I can pass already created object to WicketTester

    @Unit MyWebPage page = new MyWebPage();
    wicketTester.startPage(page);
    

    but usually I would call startPage with class name.

  2. I think AtUnit expects as well that a target object is market with @Inject so AtUnit can create and manage it - but I get an org.apache.wicket.WicketRuntimeException: There is no application attached to current thread main. Can I instruct AtUnit to use application from wicketTester?

  3. Because I don't use @Inject at MyWebPage (I think) all object that should be injected by Guice are null (in my example the service reference is null)

I really can't find anything about AtUnit inside Wicket environment. Am I doing something wrong, am I missing something?

tshepang
  • 12,111
  • 21
  • 91
  • 136
kamiseq
  • 11
  • 2

1 Answers1

0

I don't know AtUnit but I use wicket with guice and TestNG. I imagine that AtUnit should work the same way. The important point is the creation of the web application with the use of guice.

Here how I bind all this stuff together for my tests.

I have an abstract base class for all my tests:

public abstract class TesterWicket<T extends Component> {

 @BeforeClass
    public void buildMockedTester() {
        System.out.println("TesterWww.buildMockedTester");
        injector = Guice.createInjector(buildModules());
        CoachWebApplicationFactory instance = 
            injector.getInstance(CoachWebApplicationFactory.class);
        WebApplication application = instance.buildWebApplication();
        tester = new WicketTester(application);
    }

protected abstract List<Module> buildModules();

The initialization is done for every test class. The subclass defines the necessary modules for the test in the buildModules method.

In my IWebApplicationFactory I add the GuiceComponentInjector. That way, after all component instantiation, the fields annotated with @Inject are filled by Guice:

public class CoachWebApplicationFactory implements IWebApplicationFactory {

  private static Logger LOG = LoggerFactory.getLogger(CoachWebApplicationFactory.class);

  private final Injector injector;

  @Inject
  public CoachWebApplicationFactory(Injector injector) {
      this.injector = injector;
  }

  public WebApplication createApplication(WicketFilter filter) {
    WebApplication app = injector.getInstance(WebApplication.class);
    Application.set(app);
    app.addComponentInstantiationListener(new GuiceComponentInjector(app, injector));
    return app;
  }
} 
GaetanZ
  • 2,819
  • 3
  • 21
  • 20
  • ok, thanks for sharing this, still thinks that AtUnit it is not best lib to use with WicketTester. I think I will follow your way. – kamiseq Jun 24 '11 at 16:11