Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

It's possible to have your annotated dependencies automatically injected on construction. For this you have to install a SpringComponentInjector in your application.

Example:

Code Block
class MyAppliactionMyApplication extends WebApplication {
    public void init() {
        // notice in 2.0+ versions this is no longer necessary, 
        // simply drop the wicket-spring jar onto the classpath and start using @SpringBean
        addComponentInstantiationListener(new SpringComponentInjector(this));
    }
}

class EditContact extends WebPage {
   @SpringBean
   private ContactDao dao;

   @SpringBean(name="userDao")
   private UserDao userDao;
    
   public EditContact(long userId) {
       ...
   }
}

...

Code Block
public class DeleteContactPageTest extends TestCase {
	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);
		
		SpringContextLocatorMock ctxLocator=new SpringContextLocatorMock(appctx);
		
		AnnotSpringInjector injector=new AnnotSpringInjector(ctxLocator);
		
		// 3. set our mock injection environment as active
		InjectorHolder.setInjector(injector);

		// 3. setup WicketTester and injector for @SpringBean
		WicketTester app=new WicketTester();
                app.getApplication().addComponentInstantiationListener(new SpringComponentInjector(app.getApplication(), appctx ));
		
                // 4. run the test
		WicketTester app=new WicketTester();
		
		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();
	}
}

...

Part 2 is where we setup a mock spring environment. We setup a mock application context using the ApplicationContextMock obgject and add all beans necessary for the test. We also create the injector we will use to inject objects in this test.

Part 3 is where we put the injector we just created into the holder objects the pages will use to retrieve the injector. This means our injector will be used to inject dependencies into the pages with @SpringBean annotated properties.the setup of WicketTester and the SpringComponentInjector which will inject our dao into classes which have the @SpringBean annotation

Part 4 is the test itself. We go through a setup of the page and a submission of its form.

...