How to dynamically set an environment variable in spring test

I am trying to run spring -test cases using spring-boot. My test class is as follows

@ContextConfiguration(initializers = TestContextInitializer.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TestServiceApplication.class})

public class SampleTest {
    @org.junit.Test
    public void getContactsByName() throws Exception {
    }

}

So far, my configuration class looks like

public class TestContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        System.setProperty("DATA_DB_URL","some_url");
        System.setProperty("DATA_DB_USER","some_user");
        System.setProperty("DATA_DB_PASSWORD","some_password");
        System.setProperty("DATA_DB_POOL_SIZE","2");
        System.setProperty("DATA_DB_ROW_PREFETCH_SIZE","50");
    }
}

Everything is working fine, but I have problems. I cannot register PASSWORD in the source code as my company policy. How can I export the password so that I do not need to verify it.

+4
source share
2 answers

You can provide a password (or any Spring property) at run time through System or Environment or Command Line variables. All of these sources are (a) defined at runtime and (b) external to your code base.

For instance:

  • export password=...; java -jar app.jar password, Spring
  • java -Dpassword=... -jar app.jar JVM, Spring
  • java -jar myapp.jar --password=... , Spring

JNDI.

.

0

. @PropertySource .

. , , , .. . . , , , , , . , , , DATA_DB_USER .

+1

Source: https://habr.com/ru/post/1685969/


All Articles