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