`
ajoo
  • 浏览: 450206 次
社区版块
存档分类
最新评论

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

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

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

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


但是在摹客蛛丝的绑匪的时候遇到了麻烦。假设我要纪录这么一段行为期待的话:
binder.bind(List.class).annotatedWith(Names.named("test")).to(ArrayList.class).in(Singleton.class);

知道摹客的期待代码会是什么样子吗?给大家瞧瞧:
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);

哈哈,爽到了吧?这还是就对应一句蛛丝文档所谓的“嵌入式领域特殊语言”。要是多来那么几句,那才叫真爽啊。

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


这个代理代码写起来不是很难。因为代码属于公司产权,所以就不贴了。不过,是不是只有我遇到过这种需要摹客蛛丝的绑匪的需求呢?
分享到:
评论
1 楼 抛出异常的爱 2007-10-11  
看的吃力,请重写一下吧

相关推荐

Global site tag (gtag.js) - Google Analytics