从jMock1自定义调用匹配器迁移到jMock2(Migrating from jMock1 custom invocation matchers to jMock2)

编程入门 行业动态 更新时间:2024-10-26 07:39:14
从jMock1自定义调用匹配器迁移到jMock2(Migrating from jMock1 custom invocation matchers to jMock2)

我正在使用JMock-2.6.0。 我有一个包含方法名称及其预期返回值的地图。 我想在使用JMock创建的模拟对象上调用一个方法。

之前我能够使用JMock 1,因为它遵循以下语法:

mockObj.stubs().method(mymap.getKey()).will(new ReturnStub(mymap.getValue()));

但我不确定,如果有办法使用JMock-2实现这一目标。

JMock-2的文档不足。

I am using JMock-2.6.0. I have a map containing the names of methods and their expected return values. I want to invoke a method on a mock object created using JMock.

Earlier I was able to this using JMock 1 as it follows following syntax:

mockObj.stubs().method(mymap.getKey()).will(new ReturnStub(mymap.getValue()));

But I am not sure, if there is a way to achieve this using JMock-2.

JMock-2's documentation is insufficient.

最满意答案

我相信这是您一直在寻找的文档:

匹配对象或方法

虽然匹配器通常用于指定可接受的参数值,但它们也可用于指定可接受的对象或方法,使用类似于jMock 1的API语法。为此,请使用您通常会引用的匹配器直接在调用计数子句中的模拟对象。 然后将子句链接在一起以定义预期的调用。

他们的例子包括:

允许在任何模拟对象上调用任何bean属性getter:

allowing (any(Object.class)).method("get.*").withNoArguments();

例如,您可以在循环中使用以下allowing ...部分来实现类似的结果。

样本测试:

接口:

public interface ThingOneI { public abstract String getData(); public abstract void setData(String data); public abstract String getRequest(); public abstract void setRequest(String request); }

IMPL:

public class ThingOne implements ThingOneI { private String data; private String request; public ThingOne() { } @Override public String getData() { return data; } @Override public void setData(String data) { this.data = data; } @Override public String getRequest() { return request; } @Override public void setRequest(String request) { this.request = request; } }

Junit测试:

import org.jmock.Expectations; import org.jmock.Mockery; import org.junit.Before; import org.junit.Test; public class ThingOneTest { Mockery context = new Mockery(); @Before public void setUp() throws Exception { } @Test public void test() { ThingOneI thingOne = context.mock(ThingOneI.class); Map<String, String> methMap = new HashMap<String, String>(); methMap.put("getData", "5"); context.checking(new Expectations() {{ for (Map.Entry<String, String> entry : methMap.entrySet()) allowing(any(ThingOneI.class)) .method(entry.getKey()) .with(any(String.class)); will(returnValue(entry.getValue())); } }}); System.out.println(thingOne.getData()); } }

I believe this is the documentation you've been looking for:

Match Objects or Methods

Although matchers are normally used to specify acceptable parameter values, they can also be used to specify acceptable objects or methods in an expectation, using an API syntax similar to that of jMock 1. To do so, use a matcher where you would normally refer to a mock object directly in the invocation count clause. Then chain clauses together to define the expected invocation.

Their example includes:

To allow invocations of any bean property getter on any mock object:

allowing (any(Object.class)).method("get.*").withNoArguments();

For example you can use the following allowing... portion in a loop to achieve a similar result.

An sample test:

Interface:

public interface ThingOneI { public abstract String getData(); public abstract void setData(String data); public abstract String getRequest(); public abstract void setRequest(String request); }

Impl:

public class ThingOne implements ThingOneI { private String data; private String request; public ThingOne() { } @Override public String getData() { return data; } @Override public void setData(String data) { this.data = data; } @Override public String getRequest() { return request; } @Override public void setRequest(String request) { this.request = request; } }

Junit test:

import org.jmock.Expectations; import org.jmock.Mockery; import org.junit.Before; import org.junit.Test; public class ThingOneTest { Mockery context = new Mockery(); @Before public void setUp() throws Exception { } @Test public void test() { ThingOneI thingOne = context.mock(ThingOneI.class); Map<String, String> methMap = new HashMap<String, String>(); methMap.put("getData", "5"); context.checking(new Expectations() {{ for (Map.Entry<String, String> entry : methMap.entrySet()) allowing(any(ThingOneI.class)) .method(entry.getKey()) .with(any(String.class)); will(returnValue(entry.getValue())); } }}); System.out.println(thingOne.getData()); } }

更多推荐

本文发布于:2023-07-31 02:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   Migrating   matchers   invocation   custom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!