浏览 1760 次
已锁定 主题:摹客测试蛛丝程序中的绑匪
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-10-11
最近工作需要,发现要写代码扩展“蛛丝”来实现一个春天遗留系统的移植。基本上就是围绕着春天的“应用上下文”和蛛丝的绑匪/模块来上下其手。 在测试的的时候,习惯性地选择用“简单摹客”来测试行为。摹客春天的应用上下文和豆子工厂真是简单啊: 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); 这个代理代码写起来不是很难。因为代码属于公司产权,所以就不贴了。不过,是不是只有我遇到过这种需要摹客蛛丝的绑匪的需求呢? ![]() 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-10-11
看的吃力,请重写一下吧
|
|
返回顶楼 | |