如何解决如何模拟系统环境
我正在使用 Java 8 , Junit5 和 Mockito 。有没有办法模拟 System.env()?
我有以下代码来自外部依赖项,无法修改。
这里看到了几个示例,但它们引用了 Junit4 。
或者要求使用我不想使用的 PowerMock 。
有没有办法模拟这个问题或解决这个问题?请指教。谢谢。
使用遗留类和方法的我类中的方法
public class MyClass {
void myMethod() {
// I need to use this legacyMethod
Map<String,String> map = ExternalClass.externalMethod();
// some logic
}
}
我无法更改的外部课程。
public class ExternalClass {
public static Map<String,String> externalMethod() {
// other logic
// I want to mock this so that the variable name can get a value out of this.
String name = System.getenv("SOME_ENV_VALUE");
if(name == null) {
// test fails cos I end up inside here currently.
throw new RuntimeException();
}
// other logic
return new HashMap<>();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。