0

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?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
badera
  • 1,495
  • 2
  • 24
  • 49

1 Answers1

0

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 {
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • But it does work... Can you show what you mean by having a test profile? What I need is just a simple way to decide by a spring profile whether some specific test classes are executed or not. – badera Nov 15 '18 at 06:44
  • @badera you exclude tests using testng/junit tools. add test class with profile – Ori Marko Nov 15 '18 at 06:45
  • Can you show how? - the source of decision should be if a spring profile is set or not – badera Nov 15 '18 at 06:48
  • I am very confused because `@ActiveProfiles` sets the current profile and does not make the execution of tests depending on, if the listed profile is set or not. At least this is what I actually observe to happen and what is also written in https://stackoverflow.com/a/33042872/4106030 – badera Nov 15 '18 at 06:51
  • @badera the comment you asked in your question is only about `@Profile` which isn't present in my test class – Ori Marko Nov 15 '18 at 06:53