I would like to to post about how difficult is being unit test some of my code with Wicket and get some feedback about it.

First of all, I don't think the wicket-examples does a very good job on how to unit test wicket code, since having a embedded Jetty instance means integration tests. The only code in wicket-examples that does help with unit tests is inside the displaytag example (SortableTableHeadersTest), which uses MockWebApplication.

Of course the fact that dependency injection is not supported on pages provides little help, but I don't think this is the worst thing. I have a very simples class, that is supposed to interact with a Component. I can't mock this interaction, not even using cglib with jmock, because most methods are final. I am testing the following method:

    public void doAction() {
        MyObject object = (MyObject) _component.getModelObject();
        _service.save(piloto);
        _component.info("This was saved: " + object.getName());
    }

The test is something like:

    public void testDoAction() {
        _mockComponent.stubs().method("getModel").will(returnValue(new Model((Serializable) _myObject)));
        _mockService.expects(once()).method("save").with(eq(_myObject));
        _mockComponent.expects(once()).method("info").with(contains(_name));
        _context.doAction();
    }

The notes on the code above are:

I had to "stub" getModel, because the method I actually expect to be called (getModelObject) is final, and it uses getModel, that it isn't. But Component.info is final as well, and calls getPage (also final), which calls findPage (final), which calls findParent (final), so it's impossible to set any expectations neither stub any method. The tests always fails, because the Component does not find its page.

I would like to know if other are facing the same problems, or whether I am doing the wrong kind of development with Wicket, and whether I am better to go another way.

Reply via email to