- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
诚然,我是 JMockit 的新手,但出于某种原因,我在模拟 System.getProperties() 时遇到了麻烦。感谢以下帖子的帮助:
我可以使用 JMockit 1.12 成功模拟 System.getProperty():
@Test
public void testAddSystemProperty_String() {
final String propertyName = "foo";
final String propertyValue = "bar";
new Expectations(System.class) {{
System.getProperty(propertyName);
returns(propertyValue);
}};
assertEquals(propertyValue, System.getProperty(propertyName));
}
但是用于模拟 getProperties() barfs 的异常相似的代码:
@Test
public void testAddSystemProperty_String() {
final String propertyName = "foo";
final String propertyValue = "bar";
final Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
new Expectations(System.class) {{
System.getProperties();
returns(properties);
}};
assertEquals(1, System.getProperties().size());
}
我得到以下指向“返回”方法的异常:
Missing invocation to mocked type at this point;
please make sure such invocations appear only after
the declaration of a suitable mock field or parameter
此外,如何同时模拟这两种方法?如果我将它们放在同一个 Expectations block 中(首先是 getProperty()),那么我看不到异常,但 System.getProperties() 返回真实的系统属性,而不是模拟的;尽管 getProperty(propertyName) 返回模拟值。我发现这种行为非常古怪。
我从这篇文章中看到某些方法不能被模拟,但 System.getProperties() 不在该列表中:
JMockit NullPointerException on Exceptions block?
我还发现 2-3 年前使用 JMockit 的许多 SO 解决方案现在完全不可编译,所以显然情况发生了很大变化。
最佳答案
System.getProperties()
确实是 JMockit 1.12 中被排除在模拟之外的方法之一。当发现新的有问题的 JRE 方法时,这些被排除的方法的确切集合可能会在较新的版本中发生变化。
虽然没有必要模拟 System.getProperty(...)
或 System.getProperties()
。 System
类提供了可以替代使用的 setProperty
和 setProperties
方法。
关于java - JMockit - 模拟 System.getProperties() 时为 "Missing invocation to mocked type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26741283/
我正在查看 MSDN 作者在同一类的不同方法中使用以下代码的一些代码: if ( TypeDescriptor.GetProperties(ModelInstance)[propertyName] !
考虑以下代码。 Object obj; PropertyDescriptorCollection A = TypeDescriptor.GetProperties(obj); PropertyInfo
我有一种方法。在Java 8中,它在我的Macbook Pro M2上运行得很好。。结果是:。现在在迁移到Java 17之后,我得到了。我预计这两个版本的行为是相同的。我遗漏了什么?。我原以为这个方法
我有一种方法。在Java 8中,它在我的Macbook Pro M2上运行得很好。。结果是:。现在在迁移到Java 17之后,我得到了。我预计这两个版本的行为是相同的。我遗漏了什么?。我原以为这个方法
I have this method我有一种方法 static String getArchSuffix() { String arch = System.getProperty(&qu
System.getProperty("os.name") and System.getProperty("os.version") returning windows 10, 10.0, in wi
System.getProperty("os.name") and System.getProperty("os.version") returning windows 10, 10.0, in wi
我创建了包含用户列表的java类(称为“usersList”并包含每个值的用户名和密码), 现在我创建一个 JSP 文件并指定 JavaBeans 的范围: 我想将 usersList 获取到一
public object GetObjectToSerialize(object value, Type targetType) { var allProperties = value.
本文实例汇总了Java的System.getProperty()方法获取信息的用法。分享给大家供大家参考。具体如下: 复制代码代码如下: System.out.prin
我试图遍历类中的每个属性,输出属性的名称和值。但是我的代码没有返回任何属性。 正在循环的类: public class GameOptions { public ushort Fps;
我正在尝试编写一个通用实用程序,以便从 .NET 外部通过 COM 使用(/skip long story)。无论如何,我正在尝试向 ExpandoObject 添加属性,并且需要将 Property
我正在尝试使用 gradle 版本 4.8+ 的 Java、Serenity-BDD 项目,但应用程序没有提取 -Denvironment 和 -Dservicebranches 的 CLI 参数。我
我有一个源模块: import _ from 'underscore' import {Observable} from 'rxjs' export default function (rxfb) {
当我调用方法 System.getProperties(); 并打印它们时,它给出一个包含键值对的大列表,无需设置属性。 Java 从哪里获取那些属性来自? 最佳答案 来自托管 Java 的操作系统(
在这行代码中,我使用 getProperty 方法: PrintWriter writer = new PrintWriter("~/4413/ctrl/geo.txt".replaceFirst("
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
在一个非常庞大的代码库中,我发现了以下代码片段 System.getProperty("some stuff")。我尝试在一些 .properties 文件中查找该属性,但找不到它。你们有什么想法可以
让我大吃一惊! 根据 groovy 的文档,groovy 可以使用“getProperty”方法来获取对象的属性。所以当我想改变获取特殊对象属性的行为时,我使用一个类别类来覆盖“getProperty
这个问题可能会说明我缺乏关于 Groovy 类如何工作的知识,但我试图自己解决这个问题,但没有运气。我想在一个类上创建一个 getProperty() 方法,这样我就可以以 Groovyish 的方式
我是一名优秀的程序员,十分优秀!