easymock 设置对模拟对象方法参数的期望
我不确定如何使用 easymock 设置这种行为。为了说明这一点,我创建了一个简化的示例。 基本上,我有一个返回 void 的方法,并获取一张地图,我希望 easymock 更改地图,删除我指定的条目。
假设我有以下接口:
public interface Filter{
public void filter(Map<String,String>map);
}
和以下类:
public class MyClass {
private Filter filter;
public MyClass(Filter filter) {
this.filter = filter;
}
public Map<String,String> process(Map<String,String>map) {
filter.filter(map);
return map;
}
}
我不知道如何设置此行为:
public class MyClassTest {
/**
* Test method for {@link easy.MyClass#process(java.util.Map)}.
*/
@Test
public void testProcess() {
Map<String, String> map = new HashMap<String, String>();
map.put("one","AAA");
map.put("remove","BBB");
map.put("three","CCC");
Map<String, String> expectedRet = new HashMap<String, String>();
expectedRet.put("one","AAA");
expectedRet.put("three","CCC");
IMocksControl mockery = EasyMock.createControl();
mockery.resetToStrict();
mockery.checkOrder(true);
Filter mockFilter = mockery.createMock("filter", Filter.class);
MyClass m = new MyClass(mockFilter);
mockFilter.filter(map);
// I would like some behaviour that will remove the entry ("remove","BBB")
// how can I define that?
mockery.replay();
m.process(map);
mockery.verify();
assertEquals(expectedRet,map);
}
}
I'm not sure how to set up this sort of behaviour with easymock. To illustrate I created a simplified example.
Basically, I have a method that returns void, and take one map, and I'd like easymock to change the map, deleting the entry that I specify.
Suppose I have the following interface:
public interface Filter{
public void filter(Map<String,String>map);
}
and the following class:
public class MyClass {
private Filter filter;
public MyClass(Filter filter) {
this.filter = filter;
}
public Map<String,String> process(Map<String,String>map) {
filter.filter(map);
return map;
}
}
I couldn't figure out how to set this behaviour:
public class MyClassTest {
/**
* Test method for {@link easy.MyClass#process(java.util.Map)}.
*/
@Test
public void testProcess() {
Map<String, String> map = new HashMap<String, String>();
map.put("one","AAA");
map.put("remove","BBB");
map.put("three","CCC");
Map<String, String> expectedRet = new HashMap<String, String>();
expectedRet.put("one","AAA");
expectedRet.put("three","CCC");
IMocksControl mockery = EasyMock.createControl();
mockery.resetToStrict();
mockery.checkOrder(true);
Filter mockFilter = mockery.createMock("filter", Filter.class);
MyClass m = new MyClass(mockFilter);
mockFilter.filter(map);
// I would like some behaviour that will remove the entry ("remove","BBB")
// how can I define that?
mockery.replay();
m.process(map);
mockery.verify();
assertEquals(expectedRet,map);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该设置一个期望,即
m.process(map)
将在模拟的 Filter 上调用filter(map)
:您在上面的代码摘录中采用的路径正在尝试验证您的模拟过滤器。这不应该是您测试的目的。在您的案例中,被测试的类是
MyClass
,而不是Filter
。更新:您可以向模拟添加行为:
但是,在您的特定情况下,
Filter
接口足够简单,导致andDelegateTo
无法达到首先使用模拟Filter
。使用相同数量的代码,您可以在测试中使用具体的Filter
实例:You should set an expectation that
m.process(map)
will callfilter(map)
on the mocked Filter:The path you've taken in your code excerpt above is trying to verify your mocked filter. That shouldn't be the aim of your test. The class under test in your case is
MyClass
, notFilter
.Update: You can add behaviour to your mock:
However, in your particular case the
Filter
interface is simple enough to causeandDelegateTo
beat the purpose of using a mockFilter
in the first place. With the same amount of code, you can use a concreteFilter
instance in your test: