java - Jenkins 抛出 java.lang.NoSuchFieldException : theCaseInsensitiveEnvironment

标签 java unit-testing jenkins reflection environment-variables

我试图通过单元测试设置环境变量,当我在本地计算机上运行代码时,它工作正常。

但是当我将它部署到 Jenkins 管道上时,它开始抛出异常。

异常:-

java.lang.NoSuchFieldException: theCaseInsensitiveEnvironment

代码片段:-

Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.clear();
env.putAll(newenv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.clear();
cienv.putAll(newenv);

最佳答案

您正在解决错误的问题。你的业务代码不应该直接依赖于环境变量,中间应该有一个隔离层。将您的输入与服务代码分离,您将不再需要模拟环境变量。

关于java - Jenkins 抛出 java.lang.NoSuchFieldException : theCaseInsensitiveEnvironment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60994817/

相关文章:

c# - 将 C# 委托(delegate)转换为单元测试

c - 我们如何在 va_list 上应用非可变参数函数?

c++ - 将 Jenkins CI 用于 Qt 项目

java - Jmock/junit 测试

java - JVM如何保证使用枚举实现的单例线程安全?

java - 与所有者框架异步打开 JDialog 的问题

Jenkins Pipeline 扫描编译器警告自定义解析器被阻止

进行二分搜索时出现 java.lang.ArrayIndexOutOfBoundsException 错误?

python - 通过 pytest 使用多处理时如何测量覆盖率?

jenkins - 如何共享 Jenkins BUILD Monitor