首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringRunner忽略应用程序属性

SpringRunner忽略应用程序属性
EN

Stack Overflow用户
提问于 2018-01-27 03:20:55
回答 2查看 804关注 0票数 5

我正在尝试创建Spring集成测试,如下所示:

代码语言:javascript
复制
@RunWith(SpringRunner::class)
@ActiveProfiles(profiles = ["Test"])
@ContextConfiguration(locations = ["classpath:**/applicationContext.xml"])
open class SimpleEntityIT {...}

applicationContact.xml包含:

代码语言:javascript
复制
    <context:annotation-config/>
    <context:spring-configured/>
    <context:property-placeholder
            ignore-resource-not-found="false"
            location="classpath:application${spring.profiles.active}.properties,classpath:application.properties"/>

    <context:component-scan base-package="net.goout"/>

applicationContext已加载,但似乎大部分都被忽略了。Bean不是通过组件扫描构建的,application.properties被完全忽略,在日志中没有提到:

代码语言:javascript
复制
 2018-01-26 20:09:26,131 DEBUG Resolved location pattern [classpath:**/applicationContext.xml] to resources []
 2018-01-26 20:09:26,132 DEBUG Loaded 0 bean definitions from location pattern [classpath:**/applicationContext.xml]
 2018-01-26 20:09:26,167  INFO Refreshing org.springframework.context.support.GenericApplicationContext@aecb35a: startup date [Fri Jan 26 20:09:26 CET 2018]; root of context hierarchy
 2018-01-26 20:09:26,167 DEBUG Bean factory for org.springframework.context.support.GenericApplicationContext@aecb35a: org.springframework.beans.factory.support.DefaultListableBeanFactory@20d3d15a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory]; root of factory hierarchy
 2018-01-26 20:09:26,198 DEBUG Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
 2018-01-26 20:09:26,198 DEBUG Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
 2018-01-26 20:09:26,225 DEBUG Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
 2018-01-26 20:09:26,231 DEBUG Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'

我有什么不明白的?

编辑:非事件组件-扫描beans没有构造,但实际上没有构造beans -甚至那些由applicationContext.xml中的<bean>定义的beans也没有构造。它的内容似乎只是被忽略了,即使它被正确地找到了。

EN

回答 2

Stack Overflow用户

发布于 2018-02-02 13:54:24

根据最新的评论,我相信这个问题已经解决了,但即便如此,还是有几个快速观察和替代方案:

a)由于以下原因,您没有收到关于缺少资源的错误,我建议您确认这一点,以便尽早发现问题:

ignore-resource-not-found="false“

此外,您还可以使用以下命令:

代码语言:javascript
复制
@PropertySource(value = "xyz.properties", ignoreResourceNotFound = true)

b)完全无关,但:

代码语言:javascript
复制
@ActiveProfiles(profiles = ["Test"])

可以写成:

代码语言:javascript
复制
@ActiveProfiles({"Test","QA"}) 

or in your application-test.properties

spring.profiles.active=Test,QA

C)对于放置applicationContext.xml,也可以在web.xml中放置一个替代方案:

代码语言:javascript
复制
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/*.xml</param-value>
</context-param>

(不过,使用@ContextConfiguration指定确切位置的注释也完全可以!)

最重要的是,如果您使用的是基于注释的配置,我建议您使用Spring boot :-)

希望它能有所帮助!!

票数 2
EN

Stack Overflow用户

发布于 2018-01-30 23:23:08

您不应该自己将测试资源复制到/ applicationContext.xml /resources。允许这个到maven

代码语言:javascript
复制
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>
</build>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48468156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档