spring @ProfileValueSource + @IfProfileValue 组合使用

spring @ProfileValueSource + @IfProfileValue 组合使用

  • 这两个注解只支持 Junit4
  • 可以在 junit4 的 SpringRunner 、SpringJUnit4ClassRunner 以及 Spring JUnit4 support 的注解下使用
  • IfProfileValue 可以单独使用,默认使用 SystemProfileValueSource 进行配合

场景一、仅仅使用 @IfProfileValue

  • 使用默认的 ProfileValueSource 实现,配合 IfProfileValue 智能辨别单测执行
/**
 * 使用默认的ProfileValueSource实现,配合IfProfileValue 智能辨别单测执行
 * <p>
 * 以下单测中,因为在system properties中找不到env的环境配置,所以默认都被忽略掉
 *
 * @author wang.z.h
 */
// @SpringBootTest

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"dev"})

@IfProfileValue(name = "env", values = {"local", "dev"})
public class ProfileValueWithDefaultProfileValueSourceTest {


    @Test
    @IfProfileValue(name = "env", value = "local")
    public void runTest_on_env_equal_local() {
        System.out.println("runTest_on_env_equal_local");

        // Assert ...
    }


    @Test
    @IfProfileValue(name = "env", value = "dev")
    public void runTest_on_env_equal_dev() {
        System.out.println("runTest_on_env_equal_dev");

        // Assert ...
    }


    @Test
    @IfProfileValue(name = "env", value = "test")
    public void runTest_on_env_equal_test() {
        System.out.println("runTest_on_env_equal_test");

        // Assert ...
    }
}

输出内容

Test ignored.

Process finished with exit code 0

场景二、方法级别@IfProfileValue + 类级别 @ProfileValueSourceConfiguration 组合使用

  • 自定义 ProfileValueSource 实现,配合 IfProfileValue 智能辨别单测执行
/**
 * 自定义ProfileValueSource实现,配合IfProfileValue 智能辨别单测执行
 *
 * @author wang.z.h
 */
// @SpringBootTest

@RunWith(SpringRunner.class)
// @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"dev"})
@ProfileValueSourceConfiguration(ProfileValueWithImplProfileValueSourceTest.MyProfileValueSource.class)
public class ProfileValueWithImplProfileValueSourceTest {

    /**
     * 会输出
     * 目前配置env=dev,与我匹配,我应该被执行
     */
    @Test
    @IfProfileValue(name = "env", value = "dev")
    public void profileValueTest_when_value_matched_expect_test_should_run() {
        System.out.println("目前配置env=dev,与我匹配,我应该被执行");

        // Assert ...
    }

    /**
     * 会输出 Test ignored.
     */
    @Test
    @IfProfileValue(name = "env", value = "test")
    public void profileValueTest_when_value_not_matched_expect_test_ignore() {
        System.out.println("目前配置env=dev,与我不匹配,我不应该被执行");

        // Assert ...
    }


    /**
     * 根据自己需求,实现一个自己的ProfileValueSource
     *
     * @author wang.z.h
     */
    static class MyProfileValueSource implements ProfileValueSource {

        private static Map<String, String> configMap;

        static {
            configMap = new HashMap<>();
            configMap.put("test-groups", "unit-tests");
            configMap.put("env", "dev");
        }

        @Override
        public String get(String key) {
            Assert.hasText(key, "'key' must not be empty");

            return configMap.getOrDefault(key, "unknown");
        }
    }
}

输出内容

Test ignored.
目前配置env=dev,与我匹配,我应该被执行

场景三、方法级别@IfProfileValue + 类级别 @ProfileValueSourceConfiguration + 类级别@IfProfileValue 组合使用

  • 【重点】类级别的注解值,覆盖方法级别的注解值
/**
 * 【重点】类级别的注解值,覆盖方法级别的注解值
 * <p>
 * 使用 自定义的 TestProfileValueSource 实现,配合IfProfileValue 智能辨别单测执行
 * <p>
 * 以下单测中,因为在 TestProfileValueSource 中 配置了env=dev 的配置;但是又在类级别重新定义了IfProfileValue
 * 所以,最终符合 TestProfileValueSource 中 env=dev 和 @IfProfileValue(name = "env", values = {"local", "dev"}) 的并集的单侧配置才能执行
 * 即:{"dev"} ∪ {"local", "dev"} = {"dev"},那么单测中的配置value中包含 dev 的单测可以命中执行
 *
 * @author wang.z.h
 */
// @SpringBootTest

@RunWith(SpringJUnit4ClassRunner.class)
// @ActiveProfiles({"dev"})
// 使用自定义的配置
@ProfileValueSourceConfiguration(TestProfileValueSource.class)

// 基于使用自定义配置的基础上,又加了一层类级别的配置过滤
@IfProfileValue(name = "env", values = {"local", "dev"})
public class ProfileValueWithClassOverrideTest {


    @Test
    @IfProfileValue(name = "env", value = "local")
    public void runTest_on_env_equal_local() {
        System.out.println("runTest_on_env_equal_local");

        // Assert ...
    }


    @Test
    @IfProfileValue(name = "env", value = "dev")
    public void runTest_on_env_equal_dev() {
        System.out.println("runTest_on_env_equal_dev");

        // Assert ...
    }


    @Test
    @IfProfileValue(name = "env", value = "test")
    public void runTest_on_env_equal_test() {
        System.out.println("runTest_on_env_equal_test");

        // Assert ...
    }

    @Test
    @IfProfileValue(name = "env", values = {"test", "dev"})
    public void runTest_on_env_equal_test_or_dev() {
        System.out.println("runTest_on_env_equal_test_or_dev");

        // Assert ...
    }
}

输出内容

Test ignored.
runTest_on_env_equal_dev

Test ignored.
runTest_on_env_equal_test_or_dev

Process finished with exit code 0

资料