使用junit&spring修改系統的環境變數,解決docker程式測試問題

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

1,修改系統內容變數

首先環境變數在java啟動並執行時候是修改不了的。
已經設定成唯讀了雖然方法都能調用。
這個有啥用呢?因為docker開放的應用程式的環境變數都是這樣設定的。
docker在啟動的時候設定了環境變數,然後應用程式就可以直接調用了。
調用的方法java就是通過 System.getenv()獲得的。
有spring的程式,直接使用${jdbc.url}寫在xml的設定檔就好。
spring已經支出從系統內容變數裡面獲得參數了。

System.out.println(System.getenv("test.env"));//如果使用:會報錯異常。System.getenv().put("test.env","1234");

2,解決辦法

http://stackoverflow.com/questions/8168884/how-to-test-code-dependent-on-environment-variables-using-junit
使用一個開源架構:
https://github.com/stefanbirkner/system-rules
增加maven配置:這兩個包必須引用,否則類找不到。

        <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,代碼編寫

將兩個maven配置修改後就可以測試了。

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", "value");    assertEquals("value", System.getenv("name"));  }}

可以直接修改系統的環境變數。

5, 和spring結合

但是,但是,對應普通函數是可以的,但是對於spring的函數是不行的。
因為在這個類裡面進行配置之後,spring已經啟動完成了。還是沒有獲得環境變數。
解決辦法,建立一個自己的MySpringJUnit4ClassRunner 類:

import org.junit.Rule;import org.junit.contrib.java.lang.system.EnvironmentVariables;import org.junit.runners.model.InitializationError;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;public class MySpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {    @Rule    public final EnvironmentVariables environmentVariables            = new EnvironmentVariables();    public MySpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {        super(clazz);        //http://stefanbirkner.github.io/system-rules/#EnvironmentVariables        environmentVariables.set("name", "value");    }}

替換@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", System.getenv("name"));  }}

這樣在啟動的時候就使用了自己的類進行載入了。
在spring配置:

<bean id="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>  

在junit裡面就可以使用了。

5,總結

本文的原文串連是: http://blog.csdn.net/freewebsys/article/details/52781896 未經博主允許不得轉載。
博主地址是:http://blog.csdn.net/freewebsys

docker非常火,非常方便了,在寫程式的使用需要設定好變數。
但是這個對開發測試弄起來就比較麻煩了。
使用junit & srping 就可以了,可以在程式裡面寫死設定檔。
從而不影響代碼結構,不影響系統部署。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.