Mock对象只能调用stubbed方法,调用不了它真实的方法。但Mockito可以监视一个真实的对象,这时对它进行方法调用时它将调用真实的方法,同时也可以stubbing这个对象的方法让它返回我们的期望值。另外不论是否是真实的方法调用都可以进行verify验证。和创建mock对象一样,对于final类、匿名类和Java的基本类型是无法进行spy的。
监视对象
监视一个对象需要调用spy(T object)方法,如:List spy = spy(new LinkedList());那么spy变量就在监视LinkedList实例。
被监视对象的Stubbing
stubbing被监视对象的方法时要慎用when(Object),如:
List spy = spy(new LinkedList());
//Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty)
when(spy.get(0)).thenReturn("foo");
//You have to use doReturn() for stubbing
doReturn("foo").when(spy).get(0);
当调用when(spy.get(0)).thenReturn("foo")时,会调用真实对象的get(0),由于list是空的所以会抛出IndexOutOfBoundsException异常,用doReturn可以避免这种情况的发生,因为它不会去调用get(0)方法。
下面是官方文档给出的例子:
@Test
public void spyTest2() {
List list = new LinkedList();
List spy = spy(list);
//optionally, you can stub out some methods:
when(spy.size()).thenReturn(100);
//using the spy calls real methods
spy.add("one");
spy.add("two");
//prints "one" - the first element of a list
System.out.println(spy.get(0));
//size() method was stubbed - 100 is printed
System.out.println(spy.size());
//optionally, you can verify
verify(spy).add("one");
verify(spy).add("two");
}
分享到:
相关推荐
赠送jar包:mockito-core-4.0.0.jar; 赠送原API文档:mockito-core-4.0.0-javadoc.jar; 赠送源代码:mockito-core-4.0.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-4.0.0.pom; 包含翻译后的API文档:...
赠送jar包:mockito-core-4.0.0.jar; 赠送原API文档:mockito-core-4.0.0-javadoc.jar; 赠送源代码:mockito-core-4.0.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-4.0.0.pom; 包含翻译后的API文档:...
mockito-all-1.10.19.jar mockito-core-4.5.1.jar mockito-junit-jupiter-4.5.1.jar mockito-junit-jupiter-4.5.1-sources.jar
赠送jar包:mockito-core-3.8.0.jar; 赠送原API文档:mockito-core-3.8.0-javadoc.jar; 赠送源代码:mockito-core-3.8.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.8.0.pom; 包含翻译后的API文档:...
赠送jar包:mockito-core-3.9.0.jar; 赠送原API文档:mockito-core-3.9.0-javadoc.jar; 赠送源代码:mockito-core-3.9.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.9.0.pom; 包含翻译后的API文档:...
赠送jar包:mockito-core-3.1.0.jar; 赠送原API文档:mockito-core-3.1.0-javadoc.jar; 赠送源代码:mockito-core-3.1.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.1.0.pom; 包含翻译后的API文档:...
赠送jar包:mockito-core-2.15.0.jar; 赠送原API文档:mockito-core-2.15.0-javadoc.jar; 赠送源代码:mockito-core-2.15.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-2.15.0.pom; 包含翻译后的API文档...
mockito-all-1.7
mockito-all-1.10.5
总的来说,`mockito-1.9.5.zip` 包含了使用Mockito进行单元测试所需的一切,包括Mockito库本身、相关的文档和源代码,以及可能的其他依赖信息。通过这个压缩包,开发者可以轻松地在项目中集成Mockito和JUnit,编写出...
赠送jar包:mockito-core-2.15.0.jar; 赠送原API文档:mockito-core-2.15.0-javadoc.jar; 赠送源代码:mockito-core-2.15.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-2.15.0.pom; 包含翻译后的API文档...
mockito-core-3.6.0.jarmockito-core-3.6.0.jar
mockito-core-3.1.0
mockito-all-1.10.7
mockito-core-3.8.0
mockito-core-3.5.15
赠送jar包:mockito-core-3.8.0.jar; 赠送原API文档:mockito-core-3.8.0-javadoc.jar; 赠送源代码:mockito-core-3.8.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.8.0.pom; 包含翻译后的API文档:...
mockito-core-3.12.1
mockito-core-3.3.3.jarmockito-core-3.3.3.jar
mockito-junit-jupiter-3.6.0.jarmockito-junit-jupiter-3.6.0.jar