@IfProfileValue 两个 spring 个配置文件

@IfProfileValue two spring profiles

你好,我有一个 springboot 应用程序,有两个可能的 spring 配置文件,一个与环境配置相关(dev、prod、staging...),另一个模拟了一些远程服务,我们称它为 remoteServiceMock。所以我有标记为 :

的原始服务
@Profile("!remoteServiceMock")

模拟豆:

@Profile("remoteServiceMock")

除测试外一切都很好,当我 运行 申请时:

install -Dspring.profiles.active=dev,remoteServiceMock

install -Dspring.profiles.active=dev

所以加载了相应的bean。但是我在测试时遇到了问题。 我的测试 class:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@Category({IntegrationTest.class})
@TestPropertySource("classpath:test.properties")
@IfProfileValue(name = "spring.profiles.active",
        values = "remoteServiceMock")
public class DeltaServiceMockTest {

    @Autowired
    private RemoteService remoteService;

    @Test
    public void shouldIntiService() throws Exception {
        assertNotNull(remoteService);
    }

    @Test
    public void shouldGetMockDelta() throws Exception {
        RemoteData remoteDate = remoteService.getData(new Date(), new Date());
        assertEquals(15,remoteDate.getActivity().size());
    }
}

仅在 spring.profiles.active 完全匹配时才执行测试的问题。所以只有当我写:

时,测试才会 运行
@IfProfileValue(name = "spring.profiles.active", = "dev,remoteServiceMock")

问题是:

1) 是否可以使用 "contains"/"not contains" 配置文件功能

为 IfProfileValue 编写某种扩展

2) 对于我的目标还有其他更好的解决方案吗? (所以我想模拟一个(在功能上会有几个)远程服务并将我的应用程序部署到暂存环境)。

谢谢

您可能需要查看 @ActiveProfiles 功能并根据 ActiveProfilesResolver (take a look at the bottom of this Spring docs section) 对其进行自定义。