从 jMock1 自定义调用匹配器迁移到 jMock2
Migrating from jMock1 custom invocation matchers to jMock2
我正在使用 JMock-2.6.0。我有一个包含方法名称及其预期 return 值的映射。
我想在使用 JMock 创建的模拟对象上调用一个方法。
之前我可以使用 JMock 1 来实现,因为它遵循以下语法:
mockObj.stubs().method(mymap.getKey()).will(new ReturnStub(mymap.getValue()));
但我不确定是否有办法使用 JMock-2 实现此目的。
JMock-2 的文档不足。
我相信 this 是您一直在寻找的文档:
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.
他们的例子包括:
允许在任何模拟对象上调用任何 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);
}
实现:
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;
}
}
联合测试:
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());
}
}
我正在使用 JMock-2.6.0。我有一个包含方法名称及其预期 return 值的映射。 我想在使用 JMock 创建的模拟对象上调用一个方法。
之前我可以使用 JMock 1 来实现,因为它遵循以下语法:
mockObj.stubs().method(mymap.getKey()).will(new ReturnStub(mymap.getValue()));
但我不确定是否有办法使用 JMock-2 实现此目的。
JMock-2 的文档不足。
我相信 this 是您一直在寻找的文档:
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.
他们的例子包括:
允许在任何模拟对象上调用任何 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);
}
实现:
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;
}
}
联合测试:
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());
}
}