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");
}
}
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";
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With