Unit testing 忽略在spring boot中本地执行的测试
我需要禁用在本地环境上执行一个单元测试。我们使用Unit testing 忽略在spring boot中本地执行的测试,unit-testing,spring-boot,Unit Testing,Spring Boot,我需要禁用在本地环境上执行一个单元测试。我们使用localtestprofile在本地运行单元测试 -Dspring.profiles.active=local-test 我需要在除本地测试之外的所有配置文件上执行测试如何实现这一点 这是我的测试,如果我把value=“!local test”放进去,它就不起作用了 @IfProfileValue(name = "spring.profiles.active", value = "local-test") @RunWith(SpringRunn
localtest
profile在本地运行单元测试
-Dspring.profiles.active=local-test
我需要在除本地测试之外的所有配置文件上执行测试如何实现这一点
这是我的测试,如果我把value=“!local test”
放进去,它就不起作用了
@IfProfileValue(name = "spring.profiles.active", value = "local-test")
@RunWith(SpringRunner.class)
public class EnvironmentTests {
@Test
public void TestNightTimeFormatCorrect() {
throw new RuntimeException("test exception");
}
}
我找到了一个解决办法,希望对别人有用
在这种情况下,当没有本地测试
配置文件时,每次都将执行TestNightTimeFormatCorrect
。当未设置配置文件时,也将执行此操作
@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";
}
}
}