Use junit&spring to modify the environment variables of the system to solve the Docker program test problem

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Modify the system environment variables

First, the environment variable is not modifiable when it is running in Java.
is already set to read-only, although the method can be called.
What's the use of this? This is because the environment variables for Docker's open applications are set.
Docker sets the environment variables when it starts, and the application can be called directly.
The method called Java is obtained by System.getenv ().
There is a spring program that directly uses ${jdbc.url} to write the configuration file in XML as well.
Spring has already paid for parameters from the system environment variables.

System.out.println(System.getenv("test.env"));//如果使用:会报错异常。System.getenv().put("test.env","1234");

2, workaround

Http://stackoverflow.com/questions/8168884/how-to-test-code-dependent-on-environment-variables-using-junit
Use an open source framework:
Https://github.com/stefanbirkner/system-rules
Add MAVEN configuration: These two packages must be referenced, otherwise the class cannot be found.

        <dependency>            <groupId>Junit</groupId>            <artifactid>Junit</artifactid>            <version>4.12</version>            <scope>Test</Scope>        </Dependency>        <!--https://github.com/stefanbirkner/system-rules --        <dependency>            <groupId>Com.github.stefanbirkner</groupId>            <artifactid>System-rules</artifactid>            <version>1.16.1</version>            <scope>Test</Scope>        </Dependency>

3, code writing

After modifying the two MAVEN configurations, you can test them.

import  org.junit.Test; import  org.junit.Rule; import  org.junit.contrib.java.lang.system.environmentvariables;public  void  environmentvariablestest { @Rule  public  final  Environmentvariables environmentvariables = new  environmentvariables ();  @Test  public  void  setenvironmentvariable  () {Environmentvariables.set ( "name" , ); Assertequals ( "value" , system.getenv ( "name" )); }}

You can modify the environment variables of the system directly.

5, combined with spring

However, it is possible to correspond to normal functions, but not for spring functions.
Since the configuration is in this class, spring has started to complete. The environment variable is still not available.
Workaround, create your own Myspringjunit4classrunner class:

ImportOrg.junit.Rule;ImportOrg.junit.contrib.java.lang.system.EnvironmentVariables;ImportOrg.junit.runners.model.InitializationError;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner; Public  class myspringjunit4classrunner extends springjunit4classrunner {    @Rule     Public FinalEnvironmentvariables Environmentvariables =NewEnvironmentvariables (); Public Myspringjunit4classrunner(Class<?> clazz)throwsInitializationerror {Super(Clazz);//http://stefanbirkner.github.io/system-rules/#EnvironmentVariablesEnvironmentvariables.set ("Name","Value"); }}

Replace the @runwith (Myspringjunit4classrunner.class).

import org. JUnit. Test;import org. JUnit. Rule;import org. JUnit. Contrib. Java. Lang. System. Environmentvariables;import org. Springframework. Test. Context. Contextconfiguration;import org. Springframework. Test. Context. JUnit4. Springjunit4ClassRunner;@RunWith (Myspringjunit4classrunner. Class) @ContextConfiguration (locations = {"Classpath*:meta-inf/spring/*.xml"}) public void Environmentvariablestest {@Test public void setenvironmentvariable () {assertequals ("Value", the System. getenv("Name"));}}

This will use your own class to load at startup.
In Spring configuration:

<beanID="DataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource"Destroy-method="Close"> < Property name="Driverclass"Value="${jdbc.driverclass}"/> < Property name="Jdbcurl"Value="${jdbc.url}"/> < Property name="User"Value="${jdbc.user}"/> < Property name="Password"Value="${jdbc.password}"/> </bean>

It can be used in JUnit.

5, summary

The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/52781896 not allowed to reprint without the Bo master.
Bo main address is: Http://blog.csdn.net/freewebsys

Docker is very hot, very convenient, in writing the use of the program needs to set a good variable.
But it's a bit of a hassle to get a development test.
With JUnit & srping, you can write a dead configuration file inside the program.
This does not affect the code structure and does not affect system deployment.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.