Use @Profile to decide to execute test class

2019-03-03 08:04发布

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.

There is stated:

@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?

1条回答
不美不萌又怎样
2楼-- · 2019-03-03 08:24

It's true because @Profile effects Spring components and not connected to Test framework.

Nevertheless, you can have test profile which will load Spring components ( as Configuration classes) when you running tests

Example of Test class with profile:

// load related configuration classes
@ContextConfiguration(classes = { TestConfiguration.class }) 
@ActiveProfiles(profiles = { "testing" })
public class MyTest extends AbstractTestNGSpringContextTests {
查看更多
登录 后发表回答