Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 忽略在spring boot中本地执行的测试_Unit Testing_Spring Boot - Fatal编程技术网

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";
        }

    }
}