21

I have two profiles: dev and default. And I would like to skip some (not all) tests when the active profile is default. Is it possible to mark these tests somehow to do so? Or how can this be achieved? I use springboot. This is my parent test class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyServiceStarter.class, webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT,
        properties = {"flyway.locations=filesystem:../database/h2", "server.port=9100", "spring.profiles.default=dev"})
@Category(IntegrationTest.class)
public abstract class AbstractModuleIntegrationTest { ... }
IKo
  • 4,998
  • 8
  • 34
  • 54

7 Answers7

18

My colleague found a solution: so if you need to annotate separate tests you can use the @IfProfileValue annotation:

@IfProfileValue(name ="spring.profiles.active", value ="default")
    @Test
    public void testSomething() {
        //testing logic
    }

This test will run only when default profile is active

UPDATE: For Junit 5 use:

@EnabledIfSystemProperty(named = "spring.profiles.active", matches = "default")

More info: https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#integration-testing-annotations-meta

IKo
  • 4,998
  • 8
  • 34
  • 54
4

Yes you can do it.

For example use @ActiveProfiles:

@ActiveProfiles("default")
@RunWith(SpringRunner.class)
@SpringBootTest
public class YourTest {
   //tests
}
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • but this will deactivate all tests in the class. Is it possible to deactivate only several tests? – IKo May 08 '17 at 15:35
  • 4
    This is wrong. "@IfProfileValue" does not decide whether a test runs or not. It only decides which beans definition profiles should be used when the test runs. "@IfProfileValue" should be used as in the accepted answer. For detailed explanation see https://stackoverflow.com/questions/23607489/ifprofilevalue-vs-activeprofiles-in-the-context-of-spring-test/ – Ahmad Abdelghany Apr 23 '20 at 14:44
3

@IfProfileValue only works for JUnit 4. If you're on JUnit 5, as you should be by this time, use @EnabledIf or @DisabledIf.

Example:

@DisabledIf(
    expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
    reason = "Disabled on Mac OS"
)

See the docs for more details.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
3

If you want to run the tests from the command line using below:

SPRING_PROFILES_ACTIVE=dev ./gradlew test

and none of the above works for you, you can use below annotation (on a class or single test method):

@DisabledIfEnvironmentVariable(named = "SPRING_PROFILES_ACTIVE", matches = "(dev|default|local)")

The test will be disabled if the spring profile is set to dev or default or local (regular expression)

Fenio
  • 3,528
  • 1
  • 13
  • 27
1

You can use this profile based condition:

@EnabledIf(value = "#{'${spring.profiles.active}' == 'test'}", loadContext = true)
Shtefan
  • 742
  • 12
  • 14
0

Here is another alternative for JUnit 5.9.x. This will only work on test methods and not on the class level as in that case the isProfileActive needs to be static . :

@SpringBootTest
public class SomeTest {

    @Autowired
    Environment environment;

    @Test
    @DisabledIf("isProfileActive")
    void testMethod() {}

    boolean isProfileActive() {
        return Arrays.stream(this.environment.getActiveProfiles()).toList().contains("myprofile");
    }
}
Jurgen R
  • 81
  • 1
  • 3
0

None of the answers have worked for me, and look incorrect at a glance (e.g. comparing a list to a string or expecting profiles to be declared via the one system property)

The following actually works:

@EnabledIf(expression = "#{environment.acceptsProfiles('preprod')}", loadContext = true)

Unfortunately, as of Spring 5.3.23 (that I happen to be using) Environment::acceptsProfiles(String...) is marked as deprecated in favour of Environment::acceptsProfiles(Profiles...). However the Profiles of(String...) doesn't work in an expression and leads to the following error.

Property or field 'Profiles' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid

If someone knows a way around this, please correct my answer.

Ubeogesh
  • 1,633
  • 2
  • 15
  • 22