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);
这个代理代码写起来不是很难。因为代码属于公司产权,所以就不贴了。不过,是不是只有我遇到过这种需要摹客蛛丝的绑匪的需求呢?
- 浏览: 179858 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
动态properties转换
恩,不错,method.getReturnType 都用上了。动态代理的价值全被 ...
-- by upyaya -
动态properties转换
apache beanutils中 dwr中 jsf myfaces中 都有基 ...
-- by kimmking -
动态properties转换
签名,泛型,代理 都是自己平时很少用到的,运行一下,学习一下
-- by soleghost -
动态properties转换
呵呵,精益求精啊,不过有个地方参数顺序好像错了: public final c ...
-- by racnow -
动态properties转换
感觉可以参考spring里面的类型转换吧,它好像用的是java的Property ...
-- by yufu






评论排行榜