Wicket 测试 - AnnotApplicationContextMock - 没有应用程序附加到当前线程主线程

发布于 2024-08-31 11:56:41 字数 7038 浏览 4 评论 0原文

我已经为一个小型 Web 应用程序编写了几个测试,但是当我尝试运行使用 WicketTester 的页面特定测试时,出现错误。

Google 将我发送到 Apache Wicket 的邮件列表,其中用户遇到了相同的异常。他/她说问题是 AnnotApplicationContextMock 在 Wicket 应用程序之前初始化。我也粘贴了我的 WicketApplication 类。

你们中有人以前处理过这个错误吗?我已经粘贴了下面的异常和类。

例外:

-------------------------------------------------------------------------------
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)

我的页面特定测试类:

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);
    }

}

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);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-09-07 11:56:41

我能够修复它,似乎 Wicket 邮件列表中的用户是对的,我只是直到现在才得到它。

通过将 AnnotApplicationContextMock 的初始化移至 WicketApplication getSpringComponentInjector 方法内部,我能够运行测试。

    @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);
            }
        });

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);
            }
        });
一笑百媚生 2024-09-07 11:56:41

将页面中的(Application application)放入线程池,在池中添加以下条件:
if (!Application.exists()) {
ThreadContext.setApplication(应用程序);
}

take (Application application) in page to thread pool, at pool add if:
if (!Application.exists()) {
ThreadContext.setApplication(application);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文