您的位置:首页 > 运维架构 > Docker

使用junit&spring修改系统的环境变量,解决docker程序测试问题

2016-10-10 21:28 1046 查看

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 就可以了,可以在程序里面写死配置文件。

从而不影响代码结构,不影响系统部署。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  junit spring docker
相关文章推荐