@IfProfileValue两个弹簧轮廓



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

@Profile("!remoteServiceMock")

和模拟豆:

@Profile("remoteServiceMock")

当我用运行应用程序时,除了测试之外,一切都很好

install -Dspring.profiles.active=dev,remoteServiceMock

install -Dspring.profiles.active=dev

因此加载了相应的bean。但我在测试方面遇到了问题。我的测试课程:

@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) 是否可以为具有"包含"/"不包含"配置文件特征的IfProfileValue编写某种扩展

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

感谢

您可能需要研究@ActiveProfiles特性,并基于ActiveProfilesResolver对其进行自定义(请参阅本Spring文档部分的底部)。

最新更新