简体   繁体   English

如何在春季测试中动态设置环境变量

[英]How to set environment variable dynamically in spring test

I am trying to run spring-test cases with spring-boot. 我正在尝试使用spring-boot运行弹簧测试用例。 My test class looks like as follows 我的测试课如下

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

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

}

While 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 problem. 一切正常,但我有问题。 I can not check-in PASSWORD in the source code as my company policy. 我无法按照公司政策在源代码中签入PASSWORD。 How can I externalize the password so that I don't have to check it in. 如何将密码外在化,这样我就不必检入密码。

Just use environment variables. 只需使用环境变量。 Or there is the @PropertySource annotation for using different properties for tests. 或者有@PropertySource批注,用于使用不同的属性进行测试。

But on another note you really shouldn't use System Properties like that. 但是,请注意,您实际上不应该那样使用系统属性。 The System properties is for the system, what's the OS, time system, language, etc. It shouldn't be anything to do with your application. 系统属性用于系统,什么是操作系统,时间系统,语言等。它与您的应用程序无关。 This also introduces global shared state. 这也引入了全局共享状态。 Although you probably don't if you were to start running your tests in parallel they would break sporadically if you change these values from test to test. 尽管您可能不打算并行运行测试,但是如果您在测试之间更改这些值,它们可能会偶尔中断。 You could run into other problems such as the a situation where you need the property of DATA_DB_USER to be different for 2 different components. 您可能会遇到其他问题,例如需要两个不同组件的DATA_DB_USER属性不同的情况。

You can supply the password (or any Spring property) at runtime via System or Environment or Command Line variables. 您可以在运行时通过System或Environment或Command Line变量提供密码(或任何Spring属性)。 All of those sources are (a) defined at runtime and (b) external to your code base. 所有这些源都(a)在运行时定义,并且(b)在代码库外部。

For example: 例如:

  • export password=...; java -jar app.jar export password=...; java -jar app.jar sets an environment variable named password which will then be present in your Spring Environment export password=...; java -jar app.jar设置一个名为password的环境变量,该变量随后将出现在您的Spring Environment中
  • java -Dpassword=... -jar app.jar sets a JVM system parameter which will then be present in your Spring Environment java -Dpassword=... -jar app.jar设置一个JVM系统参数,该参数随后将出现在您的Spring Environment中
  • java -jar myapp.jar --password=... sets a command line variable which will then be present in your Spring Environment java -jar myapp.jar --password=...设置一个命令行变量,该变量随后将出现在您的Spring Environment中

You can even source a property from JNDI. 您甚至可以从JNDI获得资源。

More details in the docs . 在文档中有更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM