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

104 篇文章 9 订阅
4 篇文章 0 订阅

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 就可以了,可以在程序里面写死配置文件。
从而不影响代码结构,不影响系统部署。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值