你好,我有一个 springboot 应用程序,有两种可能的 spring 配置文件,一种与环境配置相关(dev、prod、staging...),另一种带有模拟的某些远程服务,我们称之为remoteServiceMock。所以我有标有的原始服务:
@Profile("!remoteServiceMock")
和模拟 bean:
@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)对于我的目标还有其他更好的解决方案吗? (所以我想模拟一个(在功能上有几个)远程服务并将我的应用程序部署到临时环境)。
谢谢
最佳答案
您可能需要研究@ActiveProfiles
功能并根据 ActiveProfilesResolver
(take a look at the bottom of this Spring docs section) 对其进行自定义。 。
关于java - @IfProfileValue 两个 Spring 轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021384/