我的 TestSuite 有两种配置,它们提供不同的注入(inject) bean。只要我用注释设置我的个人资料,这就有效。
@ActiveProfiles(profiles={"a"})
和 @ActiveProfiles(profiles={"b"})
但我似乎无法从属性源文件中设置它
我的注释看起来像
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AConfig.class, BConfig.class })
@PropertySource("classpath:/application.properties")
@TestPropertySource(locations = {"classpath:/application.properties"})
public abstract class AbstractTestIT {
...
}
而application.properties
的内容是
spring.profiles.active="a"
结果是不满足的依赖关系
如上所述,使用 @ActiveProfiles
进行的设置如上所述有效,并且使用了正确的设置。
这几乎就像 PropertySource 和/或 TestPropertySource 不适用于 @RunWith(SpringJUnit4ClassRunner.class)
最佳答案
This works so long as I set my profile with an annotation.
这是预期的。
ActiveProfiles
不依赖于 spring.profiles.active
属性。
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
作为 value
属性别名的 profiles
属性需要使用配置文件进行赋值才能激活测试
The bean definition profiles to activate.
它不使用 spring.profiles.active
属性。
spring.profiles.active
属性指定哪些配置文件在应用程序的整体配置中处于 Activity 状态,而不是在单元测试上下文中。
关于java - 带有 @RunWith(SpringJUnit4ClassRunner.class) 的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45697546/