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("[email protected]")).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.
22.2 Programmatically setting profiles You can programmatically set active profiles by calling SpringApplication. setAdditionalProfiles(...) before your application runs. It is also possible to activate profiles using Spring's ConfigurableEnvironment interface.
The default profile is always active. Spring Boot loads all properties in application. yml into the default profile. We could rename the configuration file to application-default.
Use @Profile on a Bean Let's start simple and look at how we can make a bean belong to a particular profile. We use the @Profile annotation — we are mapping the bean to that particular profile; the annotation simply takes the names of one (or multiple) profiles.
Spring Boot profiles Spring Boot allows to define profile specific property files in the form of application-{profile}. properties . It automatically loads the properties in an application. properties file for all profiles, and the ones in profile-specific property files only for the specified profile.
I found a way to active corresponding profile based on system variable or property:
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" };
}
}
then use the parameter resolver
in @ActiveProiles:
@ActiveProfiles(resolver = SpringActiveProfileResolver.class)
How to set environment variable is anther issue, and answers above have already answered it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With