我正在尝试创建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'
我没有得到什么?
编辑:没有构造事件组件扫描bean,但实际上没有构造bean - 甚至是applicationContext.xml中由<bean>
定义的bean。它的内容似乎只是被忽略了,即使它被正确找到了。
答案 0 :(得分:2)
根据最新的评论,我认为这个问题已经解决了,但即使这样,也可以快速观察和替代:
a)你没有得到遗漏资源的错误是因为下面的attr,我建议尽早确定问题:
忽略资源未找到=“假”
另外,您可以使用以下内容:
@PropertySource(value = "xyz.properties", ignoreResourceNotFound = true)
b)完全不相关但是:
@ActiveProfiles(profiles = ["Test"])
可以写成:
@ActiveProfiles({"Test","QA"})
or in your application-test.properties
spring.profiles.active=Test,QA
c)对于放置applicationContext.xml,还可以在web.xml中放置替代方法:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/*.xml</param-value>
</context-param>
(虽然使用@ContextConfiguration指定确切位置的注释也完全没问题!)
最重要的是,如果您使用基于注释的配置,我会建议Spring启动: - )
希望它有所帮助!!
答案 1 :(得分:1)
您不应自行将applicationContext.xml复制到/ test / resources。允许这个为maven
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>