浅谈jmockit中mock机制的实现 - SegmentFault
最近在工作中写单元测试的时候,有使用到jmockit来mock无关对象。
在jmockit中,你可以使用MockUp
来创建一个"fake"的实例,对某个方法指定自己的实现,而不是调用实际的方法。
对于接口类型,需要这样调用:
@Mocked private SomeInterface mockInstance; mockInstance = new MockUp<SomeInteface>() { ... }.getMockInstance();
这个倒没有什么古怪的。估计又是使用了java.reflect.Proxy。这个技巧在很多Java框架中用到,比如Spring AOP对于接口类型的实现,就是通过Proxy来混入拦截器实现的。
但是,对于其他类型的调用,就比较奇怪了:
Read full article from 浅谈jmockit中mock机制的实现 - SegmentFault
No comments:
Post a Comment