Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore test to be executed locally in spring boot

I need to disable execution of one unit test on local environment. We run unit tests in local with local-test profile

-Dspring.profiles.active=local-test

I need to execute my test at all profiles except local-test How can I achieve this?

Here is my test, if I put value = "!local-test" it does not work

@IfProfileValue(name = "spring.profiles.active", value = "local-test")
@RunWith(SpringRunner.class)
public class EnvironmentTests {

    @Test
    public void TestNightTimeFormatCorrect() {
        throw new RuntimeException("test exception");
    }
}
like image 927
Vitalii Avatar asked Feb 01 '26 00:02

Vitalii


1 Answers

I found a solution, hope it will be useful for somebody.

In this case TestNightTimeFormatCorrect will be executed every time when there is no local-test profile. It will be alse executed when no profile is set.

@RunWith(SpringJUnit4ClassRunner.class)  
@ProfileValueSourceConfiguration(EnvironmentTests.ProfileProfileValueSource.class)
@IfProfileValue(name = "local-test", value = "false")
public class EnvironmentTests {

    @Test
    public void TestNightTimeFormatCorrect() {
        throw new RuntimeException("test exception");
    }


    public static class ProfileProfileValueSource implements ProfileValueSource
    {
        @Override
        public String get(String string)
        {
            final String systemProfiles = System.getProperty("spring.profiles.active", System.getProperty("SPRING_PROFILES_ACTIVE", ""));
            final String[] profiles = systemProfiles.split(",");
            return Arrays.asList(profiles).contains(string) ? "true" : "false";
        }

    }
}
like image 62
Vitalii Avatar answered Feb 04 '26 02:02

Vitalii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!