views:

389

answers:

1

I've written a couple of tests for a small web app, but I get an error when I try to run the page specific tests that makes use of WicketTester.

Google sends me to a mailing list for Apache Wicket, where a user experienced the same exception. He/she said the problem was that AnnotApplicationContextMock was initialized before the Wicket Application. I've pasted my WicketApplication class as well.

Has any of you dealt with this error before? I've pasted the exception and the class below.

Exception:

-------------------------------------------------------------------------------
Test set: com.upbeat.shoutbox.web.TestViewShoutsPage
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.545 sec (AnnotApplicationContextMock.java:61)
    at com.upbeat.shoutbox.web.TestViewShoutsPage.setUp(TestViewShoutsPage.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:129)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:93)
    at org.unitils.UnitilsJUnit4TestClassRunner$CustomMethodRoadie.runBeforesThenTestThenAfters(UnitilsJUnit4TestClassRunner.java:168)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.unitils.UnitilsJUnit4TestClassRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:127)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
    at org.unitils.UnitilsJUnit4TestClassRunner.access$000(UnitilsJUnit4TestClassRunner.java:42)
    at org.unitils.UnitilsJUnit4TestClassRunner$1.run(UnitilsJUnit4TestClassRunner.java:87)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:94)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)

My page specific test class:

package com.upbeat.shoutbox.web;

import org.apache.wicket.application.IComponentInstantiationListener;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.spring.injection.annot.test.AnnotApplicationContextMock;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import org.unitils.spring.annotation.SpringBeanByType;

import com.upbeat.shoutbox.WicketApplication;
import com.upbeat.shoutbox.integrations.AbstractIntegrationTest;
import com.upbeat.shoutbox.persistence.ShoutItemDao;
import com.upbeat.shoutbox.services.ShoutService;
import com.upbeat.shoutbox.web.pages.ViewShoutsPage;

public class TestViewShoutsPage extends AbstractIntegrationTest {
    @SpringBeanByType
    private ShoutService svc;

    @SpringBeanByType
    private ShoutItemDao dao;

    protected WicketTester tester;

    @Before
    public void setUp() {
        final AnnotApplicationContextMock appctx = new AnnotApplicationContextMock();

        appctx.putBean("ShoutItemDao", dao);
        appctx.putBean("ShoutService", svc);

        tester = new WicketTester(new WicketApplication() {
            @Override
            protected IComponentInstantiationListener getSpringComponentInjector(WebApplication app) {
                return new SpringComponentInjector(app, appctx, false);
            }
        });
    }

    @Test
    public void testRenderPage() {
        tester.startPage(ViewShoutsPage.class);
        tester.assertRenderedPage(ViewShoutsPage.class);
        FormTester ft = tester.newFormTester("addShoutForm");
        ft.setValue("nickname", "test-nickname");
        ft.setValue("content", "a whole lot of content");
        ft.submit();
        tester.assertRenderedPage(ViewShoutsPage.class);
        tester.assertContains("test-nickname");
        tester.assertContains("a whole lot of content");    
    }
}

AbstractIntegrationTest:

package com.upbeat.shoutbox.integrations;

import org.springframework.context.ApplicationContext;
import org.unitils.UnitilsJUnit4;
import org.unitils.spring.annotation.SpringApplicationContext;

@SpringApplicationContext({"/com/upbeat/shoutbox/spring/applicationContext.xml", "applicationContext-test.xml"})
public abstract class AbstractIntegrationTest extends UnitilsJUnit4 {
    private ApplicationContext applicationContext;
}

WicketApplication:

package com.upbeat.shoutbox;

import org.apache.wicket.application.IComponentInstantiationListener;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.target.coding.IndexedParamUrlCodingStrategy;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

import com.upbeat.shoutbox.web.pages.ParamPage;
import com.upbeat.shoutbox.web.pages.VeryNiceExceptionPage;

/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see com.upbeat.shoutbox.Start#main(String[])
 */
public class WicketApplication extends WebApplication
{    
    /**
     * Constructor
     */
    public WicketApplication()
    {
    }

    /**
     * @see org.apache.wicket.Application#getHomePage()
     */
    public Class getHomePage()
    {
        return HomePage.class;
    }

    @Override
    protected void init() {
        super.init();

        // Enable wicket ajax debug
        getDebugSettings().setAjaxDebugModeEnabled(true);

        addComponentInstantiationListener(getSpringComponentInjector(this));
        // Mount pages
        mountBookmarkablePage("/home", HomePage.class);
        mountBookmarkablePage("/exceptionPage", VeryNiceExceptionPage.class);
        mount(new IndexedParamUrlCodingStrategy("/view_params", ParamPage.class));
    }

    protected IComponentInstantiationListener getSpringComponentInjector(WebApplication app) {
        return new SpringComponentInjector(app);
    }

}
+1  A: 

I was able to fix it, seems the user from the Wicket mailing list was right, I was just unable to get it right until now.

By moving the initialization of AnnotApplicationContextMock inside of the WicketApplication getSpringComponentInjector-method I was able to get the tests running.

    @Before
    public void setUp() {
        tester = new WicketTester(new WicketApplication() {
            @Override
            protected IComponentInstantiationListener getSpringComponentInjector(WebApplication app) {
                AnnotApplicationContextMock appctx = new AnnotApplicationContextMock();
                appctx.putBean("shoutItemDao", dao);
                appctx.putBean("shoutService", svc);
                return new SpringComponentInjector(app, appctx, true);
            }
        });
John