我正在尝试创建Spring集成测试,如下所示:
@RunWith(SpringRunner::class)
@ActiveProfiles(profiles = ["Test"])
@ContextConfiguration(locations = ["classpath:**/applicationContext.xml"])
open class SimpleEntityIT {...}applicationContact.xml包含:
<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被完全忽略,在日志中没有提到:
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也没有构造。它的内容似乎只是被忽略了,即使它被正确地找到了。
发布于 2018-02-02 13:54:24
根据最新的评论,我相信这个问题已经解决了,但即便如此,还是有几个快速观察和替代方案:
a)由于以下原因,您没有收到关于缺少资源的错误,我建议您确认这一点,以便尽早发现问题:
ignore-resource-not-found="false“
此外,您还可以使用以下命令:
@PropertySource(value = "xyz.properties", ignoreResourceNotFound = true)b)完全无关,但:
@ActiveProfiles(profiles = ["Test"])可以写成:
@ActiveProfiles({"Test","QA"})
or in your application-test.properties
spring.profiles.active=Test,QAC)对于放置applicationContext.xml,也可以在web.xml中放置一个替代方案:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/*.xml</param-value>
</context-param>(不过,使用@ContextConfiguration指定确切位置的注释也完全可以!)
最重要的是,如果您使用的是基于注释的配置,我建议您使用Spring boot :-)
希望它能有所帮助!!
发布于 2018-01-30 23:23:08
您不应该自己将测试资源复制到/ applicationContext.xml /resources。允许这个到maven
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>https://stackoverflow.com/questions/48468156
复制相似问题