Jia*_*HAI 5 continuous-integration rate-limiting redis spring-boot spring-boot-test
由于Redis的宿主机local和CI不同,我的@Tests可以本地通过,CI不能通过。
首先,我试图模拟RedisTemplate这样的:
RedisTemplate redisTemplate = mock(RedisTemplate.class);
ValueOperations valueOperations = mock(ValueOperations.class);
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
when(valueOperations.increment(anyString(), anyLong())).thenReturn(1L);
when(valueOperations.get("a@a.com")).thenReturn("1");
Run Code Online (Sandbox Code Playgroud)
它确实被嘲笑RedisTemplate,但不能嘲笑redisTemplate.opsForValue()和valueOperations.increment(...)(我找不到原因)
然后,我写了两个名为application-ci-test.ymland 的配置文件applicaiton-test.yml,尝试根据系统环境变量激活其中一个
我从这里了解到我可以通过这种方式设置活动配置文件:
@Configuration
public class MyWebApplicationInitializer
implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter(
"spring.profiles.active", "dev");
}
}
Run Code Online (Sandbox Code Playgroud)
这样:
@Autowired
private ConfigurableEnvironment env;
...
env.setActiveProfiles("someProfile");
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用它们。系统变量可以通过System.getenv(..). 所以现在我想知道如何根据我得到的变量激活配置文件。
我找到了一种基于系统变量或属性激活相应配置文件的方法:
import org.springframework.test.context.ActiveProfilesResolver;
public class SpringActiveProfileResolver implements ActiveProfilesResolver {
@Override
public String[] resolve(Class<?> testClass) {
final String isCITest = System.getEnv("isCITest");
return new String[] { isCITest == null ? "test" : "ci-test" };
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用resolver@ActiveProiles 中的参数:
@ActiveProfiles(resolver = SpringActiveProfileResolver.class)
Run Code Online (Sandbox Code Playgroud)
如何设置环境变量是另一个问题,上面的答案已经回答了
| 归档时间: |
|
| 查看次数: |
4656 次 |
| 最近记录: |