Testing Pax Wicket

Testing Pax Wicket

Unit tests witch spring and wicket alone are quite simple. They original approach is explained here. Nevertheless, since pax-wicket uses the @PaxWicketBean annotation instead the @SpringBean one also a slightly different unit-test approach is required. Though it is quite similar and simple:

public void test() throws ServletException { // 1. setup dependencies and mock objects Contact contact=new Contact(); MockControl daoCtrl=MockControl.createControl(ContactDao.class); ContactDao dao=(ContactDao) daoCtrl.getMock(); daoCtrl.expectAndReturn(dao.load(10), contact); dao.delete(10); daoCtrl.replay(); // 2. setup mock injection environment ApplicationContextMock appctx=new ApplicationContextMock(); appctx.putBean("contactDao", dao); // 3. run the test WicketTester app=new WicketTester(); app.getApplication().addComponentInstantiationListener( new PaxWicketSpringBeanComponentInjector(app.getApplication(), appctx)) app.startPage(new DeleteContactPage(new DummyHomePage(), 10)); app.assertRenderedPage(DeleteContactPage.class); app.assertComponent("confirmForm", Form.class); app.assertComponent("confirmForm:confirm", Button.class); app.setParameterForNextRequest("confirmForm:confirm", "pressed"); app.submitForm("confirmForm"); app.assertRenderedPage(DummyHomePage.class); daoCtrl.verify(); }

To reduce the required fuss the following writing is also an option:

private final WicketTester tester = new WicketTester(); private final SimplifiedPaxWicketInjector injector = registerBeanInjector(tester); // define your mock services here @Before public void before() { injector.registerBean("name", mockService); } @Test public void my_test () { // here you can be sure that all your beans had been registered }

Comments