java - @IfProfileValue 两个 Spring 轮廓

标签 java spring unit-testing spring-boot spring-profiles

你好,我有一个 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/

相关文章:

java - JSCH channel shell ,输入/输出

java - Spring 3.2使用交叉参数约束

java - 从post请求中直接获取json信息

java - 禁用 EnableGlobalMethodSecurity 注释

python - 模拟对象的多个实例

java - 在 HashMap 中搜索特定模式

mysql - Grails 中属性的自引用关系?

java - spring 如何在spring中使用多个@RequestMapping注解?

python - 模拟 itertools.zip_longest

unit-testing - 何时执行我的单元测试以及为什么使用 Moq