1

您好,我有一个 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)我的目标还有其他更好的解决方案吗?(所以我想模拟一个(功能有几个)远程服务并将我的应用程序部署到登台环境)。

谢谢

4

1 回答 1

2

您可能希望研究@ActiveProfiles功能并根据ActiveProfilesResolver(查看此 Spring 文档部分的底部)对其进行自定义。

于 2015-12-01T15:10:06.000 回答