java - 带有 @RunWith(SpringJUnit4ClassRunner.class) 的配置文件

标签 java spring spring-junit springjunit4classrunner

我的 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/

相关文章:

spring - hibernate + Spring 错误

java - Spring Cache - 如何捕获 except 参数中的异常?

Spring 我应该在每个类(class)上使用@DirtiesContext

java - 在 OpenCV for Java 中应用掩码过滤 Mat 对象

java - 主应用程序的软件集成和共享数据库

java - 将 session 工厂从一项服务传递到另一项服务

java - 使用 JUnit、Spring 4 和 Vaadin 对类进行单元测试时获取 NPE

hsqldb - Junit HSQLDB - 用户缺少权限或找不到对象 - THIS_.oh-ordnbr

java - 在一个表单中有多个提交按钮并确定哪个在 Controller 中被按下

java - 如何使用具有加密属性的 Google Guice?