Mockito 学习篇(三)
来源:互联网 发布:linux 修改时区 cst 编辑:程序博客网 时间:2024/06/08 20:07
Mockito学习篇目录:
Mockito 学习篇(一)
Mockito 学习篇(二)
Mockito 学习篇(三)
Mockito 学习篇(四)完结import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.Mockito;import org.mockito.Spy;import org.mockito.runners.MockitoJUnitRunner;import java.util.ArrayList;import java.util.HashSet;import java.util.LinkedList;import static org.mockito.AdditionalAnswers.delegatesTo;import static org.mockito.Mockito.doReturn;import static org.mockito.Mockito.mock;/** * MockitoTester. * * @author zxb * @version 1.0 * @since <pre>08/29/2017</pre> */@RunWith(MockitoJUnitRunner.class)public class MockitoTest3 { @Mock private LinkedList linkedList; @Spy private ArrayList arrayList; final class DontYouDareToMockMe extends ArrayList { } @Test public void test() throws InterruptedException { //区别一个对象是模拟对象还是侦查对象 Assert.assertTrue(Mockito.mockingDetails(linkedList).isMock()); Assert.assertTrue(Mockito.mockingDetails(arrayList).isSpy()); //模拟对象不一定是侦查对象 Assert.assertFalse(Mockito.mockingDetails(linkedList).isSpy()); //侦查对象是模拟对象的一种类型 Assert.assertTrue(Mockito.mockingDetails(arrayList).isMock()); //委托调用真实实例(doCallRealMethod),用在以下几种场景: //Final classes but with an interface,有接口(有继承应该也行)的final类 //Already custom proxied object ,已经自定义代理的对象 //Special objects with a finalize method, i.e. to avoid executing it 2 times,final方法 DontYouDareToMockMe dontYouDareToMockMe = new DontYouDareToMockMe(); dontYouDareToMockMe.add(new HashSet()); ArrayList listWithDelegate = mock(ArrayList.class, delegatesTo(dontYouDareToMockMe)); Assert.assertEquals("mock对象的size不为1", 1, listWithDelegate.size()); listWithDelegate.clear(); //委托调用了真实方法,所以真实对象的size也为空了。 Assert.assertEquals("dontYouDareToMockMe的size不为0", 0, dontYouDareToMockMe.size()); //因为是调用真实对象的方法,所以当索引不存在时,不能直接用when..thenReturn方法 //when(listWithDelegate.get(0)).thenReturn("foo"); doReturn("foo").when(listWithDelegate).get(0); Assert.assertEquals("foo", listWithDelegate.get(0)); //比如对String类mock String str = "vvv"; CharSequence mock = mock(CharSequence.class, delegatesTo(str)); System.out.println(mock); Assert.assertThat(mock.toString(), new Equals("vvv")); }}
阅读全文
0 0
- Mockito 学习篇(三)
- Mockito 学习篇(一)
- Mockito 学习篇(二)
- Mockito 学习篇(四)完结
- JUnit + Mockito 单元测试(三)
- 学习Mockito框架
- Mockito学习总结
- Mockito学习1
- MockITO学习总结
- mockito学习记录
- mockito测试入门学习
- Mockito的学习笔记
- Mockito(三)--完整功能介绍
- Mockito(一) -- 入门篇
- Mockito(二)--实例篇
- Mockito(一) -- 入门篇
- Mockito(二)--实例篇
- Mockito(一) -- 入门篇
- Git详解之八 Git与其他系统
- 打开网页渐变效果
- 简单的matlab图片分类程序
- 页面加载条百分比
- 搭建测试网站
- Mockito 学习篇(三)
- SpringMVC 通配符的匹配很全面, 但无法找到元素 'mvc:default-servlet-handler' 的声明。
- IntelliJ IDEA快捷键
- 抓包简单使用介绍
- effective C++条款四十二解读
- Git详解之九 Git内部原理
- Linux中rz和sz命令用法
- kafka管理器kafka-manager部署安装
- Java8源码-Map总结