使用@Profile 决定执行测试class

Use @Profile to decide to execute test class

根据 我们不应该使用 @Profile 让 spring 配置文件决定是否应执行或忽略测试 class 中的所有测试。

有说明:

@Profile is used to selectively enable a component (e.g., @Service, etc.), @Configuration class, or @Bean method if one of the named bean definition profiles is active in the Spring Environment for the ApplicationContext. This annotation is not directly related to testing: @Profile should not be used on a test class.

这是真的吗?如果是那么为什么?

这是真的,因为 @Profile 影响 Spring 组件并且未连接到测试框架。

不过,您可以拥有测试配置文件,当您 运行 测试

时,它将加载 Spring 组件(作为配置 classes)

测试示例 class 配置文件:

// load related configuration classes
@ContextConfiguration(classes = { TestConfiguration.class }) 
@ActiveProfiles(profiles = { "testing" })
public class MyTest extends AbstractTestNGSpringContextTests {