Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active profile in SpringBootTest based on system variable

As the host of Redis is different in local and CI, my @Tests 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.

like image 374
Jiaqi CHAI Avatar asked Dec 21 '18 11:12

Jiaqi CHAI


People also ask

How do you set spring profiles active dynamically?

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.

What is the default active profile in spring boot?

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.

How do you define beans for a specific profile?

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.

How do I run spring boot based on profile?

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.


1 Answers

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

like image 93
Jiaqi CHAI Avatar answered Sep 25 '22 00:09

Jiaqi CHAI