Use @Profile to decide to execute test class

狂风中的少年 提交于 2019-12-02 17:11:10

问题


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:


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 {


来源:https://stackoverflow.com/questions/53313489/use-profile-to-decide-to-execute-test-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!