具有setAttribute(String,Object)的JMock匹配器 [英] JMock matchers with setAttribute(String, Object)

查看:163
本文介绍了具有setAttribute(String,Object)的JMock匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在期待来自模拟的setAttribute调用时遇到问题.

I am facing issue while expecting a setAttribute call from a mock.

MyClass {
  public final void setAttribute(String name, Object value) {
  // Do Something
}

myClass.setAttribute("Key", "Value");

在调用setAttribute操作时,将String作为值传递. 我用上面的类名嘲笑了我的模拟类,并在我的Expectations块中有以下代码.

While invoking the setAttribute operation, String is passed as a value. I have a mock of the above class by the name mockMyClass and in my Expectations block I have the below code.

oneOf(mockMyClass).setAttribute(with(equal("Key")), with(equal("Value")));

我还尝试了使用any,只是为了查看泛型是否有效,但这也带来了同样的问题.

I have also tried using any, just to see if the generic works, but that is also giving the same issue.

我得到的异常:

java.lang.IllegalArgumentException:并非所有参数都被赋予显式匹配器:要么必须由匹配器指定所有参数,要么必须由值指定所有参数,您不能混合匹配器和值

java.lang.IllegalArgumentException: not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values

最初,我尝试不使用任何匹配器,但遇到了异常:

Initially I was trying without any matchers and was getting the exception:

oneOf(mockMyClass).setAttribute("Key", "Value");

org.jmock.api.ExpectationError:意外调用

org.jmock.api.ExpectationError: unexpected invocation

如何使它工作?我打算检查实际值.

How to get this working? I intend to check for actual values.

推荐答案

make方法不是最终的

make method not final

 public void setAttribute(String name, Object value) {}

并使用

oneOf(mockMyClass).setAttribute("Key", "Value");

或与匹配器any();

or with matcher any();

来自 jmock ClassImposteriser无法创建最终类的模拟或模拟最终方法.

使用jMock和ClassImposteriser模拟类,因为它使用 Java的标准反射功能,默认配置 jMock框架只能模拟接口,不能模拟类. (实际上, 我们认为这是一件好事,因为它鼓励设计 专注于对象之间的通信,而不是静态的 分类或数据存储).但是,ClassImposteriser 扩展类使用CGLIB 2.1和Objenesis库创建 模拟类以及接口的对象.这在以下情况下很有用 与遗留代码一起工作,以紧密地分离依赖关系 耦合类.

Mocking Classes with jMock and the ClassImposteriser Because it uses Java's standard reflection capability, the default configuration of the jMock framework can only mock interfaces, not classes. (Actually, we consider that to be a good thing because it encourages the design to focus on communication between objects rather than static classification or data storage). However, the ClassImposteriser extension class uses the CGLIB 2.1 and Objenesis libraries to create mock objects of classes as well as interfaces. This is useful when working with legacy code to tease apart dependencies between tightly coupled classes.

ClassImposteriser创建模拟实例,而无需调用 模拟类的构造函数.所以带有构造函数的类 可以安全地拥有对象的参数或调用可重写方法 嘲笑.但是,ClassImposteriser无法创建final的模拟 类或模拟最终方法.

The ClassImposteriser creates mock instances without calling the constructor of the mocked class. So classes with constructors that have arguments or call overideable methods of the object can be safely mocked. However, the ClassImposteriser cannot create mocks of final classes or mock final methods.

如果要模拟最终类或最终方法,请使用JDave库 包括可取消最终确定的unfinalizer Instrumentation代理 类由JVM加载之前.然后他们可以被嘲笑 ClassImposteriser.

If you want to mock final classes or final methods, the JDave library includes an unfinalizer Instrumentation agent that can unfinalise classes before they are loaded by the JVM. They can then be mocked by the ClassImposteriser.

这篇关于具有setAttribute(String,Object)的JMock匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆