使用@Profile决定执行测试类 [英] Use @Profile to decide to execute test class
问题描述
根据 https://stackoverflow.com/a/33042872/4106030
我们不应该使用@Profile
来让spring配置文件决定是应执行还是忽略测试类中的所有测试.
According to https://stackoverflow.com/a/33042872/4106030
we should not use @Profile
to let a spring profile decide whether all tests in a test class shall be executed or ignored.
有记载:
@Profile
用于有选择地启用某个组件(例如,@Service
等),@Configuration
类或@Bean
方法,如果其中一个命名的bean定义配置文件在Spring Environment中处于活动状态. ApplicationContext.此注释与测试没有直接关系:@Profile
不应在测试类上使用.
@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.
这是真的吗?如果是,那为什么呢?
Is this true? If yes then why?
推荐答案
这是真的,因为@Profile
影响Spring组件并且未连接到测试框架.
It's true because @Profile
effects Spring components and not connected to Test framework.
尽管如此,您仍可以具有测试配置文件,该配置文件将在运行测试时加载Spring组件(作为配置类)
Nevertheless, you can have test profile which will load Spring components ( as Configuration classes) when you running tests
具有配置文件的Test类的示例:
Example of Test class with profile:
// load related configuration classes
@ContextConfiguration(classes = { TestConfiguration.class })
@ActiveProfiles(profiles = { "testing" })
public class MyTest extends AbstractTestNGSpringContextTests {
这篇关于使用@Profile决定执行测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!