As the host of Redis is different in local and CI, my @Test
s can pass locally, they can't pass in CI.
Firstly, I tried to mock the RedisTemplate
like this:
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");
It did mocked RedisTemplate
, but can't mock redisTemplate.opsForValue()
and valueOperations.increment(...)
( I can't find reason )
Then, I wrote two profiles named application-ci-test.yml
and applicaiton-test.yml
, tried to active one of them based on system environment variable
I learnd from here that I can set active profile in this way:
@Configuration
public class MyWebApplicationInitializer
implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter(
"spring.profiles.active", "dev");
}
}
and this way:
@Autowired
private ConfigurableEnvironment env;
...
env.setActiveProfiles("someProfile");
But I don't know how to use them. The system variable can get by System.getenv(..)
. So now I want to know how to active profile based on the variable I get.