摹客测试蛛丝程序中的绑匪

为了响应伟大的“抵制中英文夹杂的资本主义不良思潮”运动,本文将全部用中文书写(代码暂时例外)。

最近工作需要,发现要写代码扩展“蛛丝”来实现一个春天遗留系统的移植。基本上就是围绕着春天的“应用上下文”和蛛丝的绑匪/模块来上下其手。

在测试的的时候,习惯性地选择用“简单摹客”来测试行为。摹客春天的应用上下文和豆子工厂真是简单啊:
[code]
ListableBeanFactory beanFactory = mock(ListableBeanFactory.class);
expect(beanFactory.getBeanDefinitions()).andReturn(beanNames);
replay();
...
verify();
[/code]

但是在摹客蛛丝的绑匪的时候遇到了麻烦。假设我要纪录这么一段行为期待的话:
[code]
binder.bind(List.class).annotatedWith(Names.named("test")).to(ArrayList.class).in(Singleton.class);
[/code]
知道摹客的期待代码会是什么样子吗?给大家瞧瞧:
[code]
Binder binder = mock(Binder.class);
AnnotatedBindingBuilder annotatedBuilder = mock(AnnotatedBindingBuilder.class);
LinkedBindingBuilder linkedBuilder = mock(LikedBindingBuilder.class);
ScopedBindingBuilder scopedBuilder = mock(ScopedBindingBuilder.class);
expect(binder.bind(List.class)).andReturn(annotatedBuilder);
expect(annotatedBuilder.annotatedWith(Names.named("test"))).andReturn(linkedBuilder);
expect(linkedBuilder.to(ArayList.class)).andReturn(scopedBuilder);
scopedBuilder.in(Singleton.class);
[/code]
哈哈,爽到了吧?这还是就对应一句蛛丝文档所谓的“嵌入式领域特殊语言”。要是多来那么几句,那才叫真爽啊。

为了避免爽到高潮,我就写了一个小动态代理来包装简单摹客。用了这个包装之后,期待代码就变成了简单的:
[code]
Binder binder = mockBinder();
binder.bind(List.class).annotatedWith(Names.named("test")).to(ArrayList.class).in(Singleton.class);
[/code]

这个代理代码写起来不是很难。因为代码属于公司产权,所以就不贴了。不过,是不是只有我遇到过这种需要摹客蛛丝的绑匪的需求呢? :wink:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值