zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

2023-09-11 14:21:06 时间

首先环境变量在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,代码编写
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")); }

可以直接修改系统的环境变量。


但是,但是,对应普通函数是可以的,但是对于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配置:


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里面就可以使用了。


本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/52781896 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

docker非常火,非常方便了,在写程序的使用需要设置好变量。
但是这个对开发测试弄起来就比较麻烦了。
使用junit srping 就可以了,可以在程序里面写死配置文件。
从而不影响代码结构,不影响系统部署。


修改可能对 Confluence 的后续版本不兼容,当你对 Confluence 进行升级的时候,你应该总是对你自定义修改的模板文件进行仔细的测试来确定所有的修改对新版本的 Confluence 兼容。
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载