Using Spring with Wicket Page Test

If you're using Spring beans in your Wicket pages, change the dependency from wpt-runtime to wpt-runtime-spring in pom.xml:

<project ...>

In your application class, use an injector that will inject mocks first and if not found, try the Spring beans:

public class MyApp extends WebApplication {

        protected void init() {

There is no need to change your Wicket page; you continue to use @SpringBean to inject the beans:

public class PageContainingForm extends WebPage {
        private MyService service;
        private String input;
        private String result;

        public PageContainingForm() {
                input = service.getDefaultInput();
                Form<PageContainingForm> form = new Form<PageContainingForm>("form",
                                new CompoundPropertyModel<PageContainingForm>(this)) {

                        protected void onSubmit() {
                                result = service.getResult(input);
                form.add(new TextField<String>("input"));
                add(new Label("result", new PropertyModel<String>(this, "result")));

In the test, you provide the mock as a bean to be injected instead of passing it as a constructor argument:

public class PageContainingFormTest {
        public void testSubmitForm() {
                MockableSpringBeanInjector.mockBean("service", new MyService() {

                        public String getDefaultInput() {
                                return "xyz";

                        public String getResult(String input) {
                                return input + input;

                WicketSelenium ws = WebPageTestContext.getWicketSelenium();
                assert ws.getValue("input")).equals("xyz");
                assert ws.getText("result")).equals("xyzxyz");