【问题标题】:ContextConfiguration RunWith WebAppConfiguration annotations togetherContextConfiguration Run与WebAppConfiguration注解一起
【发布时间】:2013-10-03 08:18:31
【问题描述】:

我有注释这些注释的类:

    @ContextConfiguration(locations = { "classpath:pathToXml.xml" })
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration

您能解释一下我在课堂上添加这些注释后有哪些功能吗?

【问题讨论】:

    标签: java spring testing spring-mvc


    【解决方案1】:

    您将获得 @WebAppConfiguration 配置 javadoc 中描述的功能。

    WebApplicationContext 主要改变了资源的加载方式,即具有未指定资源前缀的资源将从src/main/webapp 或value 参数中的位置加载(否则它们将在 Spring 上下文中不可用,因为通常webapp 文件夹不包含在类路径中)而不是 classpath:。

    您还可以测试使用其他 WebApplicationContext 功能的代码 - ServletContextAware beans、Session 和 Request bean 范围等。

    这意味着您将能够使用Spring MVC Test Framework

    【讨论】:

      【解决方案2】:

      因此,在与@WebAppConfiguration 注释的测试中,我注意到在创建的模拟应用程序上下文中,resourceLoader 字段为 null。所以,我创建了一个自定义的ApplicationContextInitializer,将该字段设置为AnnotationConfigApplicationContext 实例。

      然后所有需要访问常规资源的测试只需添加 @ContextConfiguration(initializers = { MyConfigurableContext.class }) 之类的内容

      似乎可以解决问题,但显然是 YMMV

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-08
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 2021-05-13
        • 2014-08-14
        • 2013-05-22
        • 1970-01-01
        相关资源
        最近更新 更多