Search code examples
springspring-bootspring-profilesintegration-testing

Run integration tests only if specific spring profile is set


We have multiple test classes in our spring boot application. Some of the classes contain integration tests, some contain unit tests. These means that if I (e.g. with maven) let all tests to be executed, it will run all tests in all classes.

What I like to achieve is that the integration tests are executed only, if a specific spring profile is set, e.g. via application.yml. I like e.g. to annotate the whole test class to define that the tests in this class are only executed if the specified spring profile is set. If it is not set, these tests shall be ignored.

  • The topic How can I use @IfProfileValue to test if a Profile is active? goes in exactly this direction. @IfProfileValue looks at first glance exactly like it is what I need. But as it is pointed out, it is not. I could use it, if I would set a specific system property. But I need to use a real spring profile (and not the system property spring.profiles.active - this would ignore a profile set via application.yml)
  • @Profile seems to look also to be what I need but as the topic Use @Profile to decide to execute test class shows, we should not use it.

So what can be done to achieve this? Note that there are a lot of questions about tests and spring profiles on stack overflow. But most of them point out how to set configurations in tests specific to spring profiles. That is not would I am looking for. I would like to execute or ignore the tests.


Solution

  • I don't know exactly how you want to achieve it, but here is a way if you are using junit to conditionally ignore some tests at runtime simply using a configuration property:

    application.properties:

    test.enabled=true
    

    then in your test code you can use org.junit.Assume and a property like the following:

    @Value("${test.enabled}")
    private Boolean testEnabled;
    
    @Test
    public void test {
        org.junit.Assume.assumeTrue(testEnabled);
        // your test code
    }
    

    now if you set the property test.enabled to true the test will run, otherwise it will be ignored.

    Source: Conditionally ignoring tests in JUnit 4