在之前的文章中我们 Mockito就是其中之一, 在分享Mockit之前, 先聊聊它处在哪个部分?

在单元测试自动化体系里有4个关键部分组成:

构建管理: Maven/Gradle项目管理和构建工作

代码管理Git/Perforce

集成和分析管理:持续集成Jenkins,代码分析Sonar

测试框架: Junit/Mockito/TestNG

Mockito是一种针对java的Mocking框架,它与EasyMock 和JMock很相似,通过在执行后校验什么已经被调用,消除了对期望行为(expections)的需要,其他的mocking库需要你在执行前记录期望行为,而导致了丑陋的初始化代码. Mockito让代码更贴近自然语言,便于阅读.

大多 Java Mock 库如 EasyMock 或 JMock 都是 expect-run-verify (期望-运行-验证)方式,而 Mockito 则使用更简单,更直观的方法, 在执行后的互动中提问。使用 Mockito,你可以验证任何你想要的 . Mockito无需准备昂贵的前期启动。他们的目标是透明的,让开发人员专注于测试选定的行为。

Mockito拥有的非常少的API,所有开始使用 Mockito,几乎没有时间成本。因为只有一种创造 mock 的方式。只要记住,在执行前 stub,而后在交互中验证。你很快就会发现这样 TDD java 代码是多么自然。

Mockito 语法流程

Mockito
Mock mock(Class classToMock); mock(Class classToMock, String name)
Stub when(mock.someMethod()).thenReturn(value) when(mock.someMethod()).thenThrow(new RuntimeException) when(mock.someMethod()).thenAnswer()
exec

首先要利用mock来构造依赖,其次利用when语句来构造stub,然后就可以执行测试方法了

如何使用Mockito

在Intellij IDEA中创建Maven项目

在pom.xml添加依赖包

        <dependency>            <groupId>org.mockito</groupId>            <artifactId>mockito-core</artifactId>            <version>2.15.0</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>        </dependency>

来看下maven导入的mockito包是什么样的

mockito_mockito中文文档_mockito静态方法

接下来就是创建测试类在src/test/java目录里

package com.portal.mock;
import org.testng.annotations.Test;
import java.util.LinkedList;import java.util.List;
import static org.mockito.Mockito.*;
public class MockTest {

@Test public void testA(){        //You can mock concrete classes, not just interfaces LinkedList mockList=mock(LinkedList.class);

//using mock mockList.add("once");
mockList.add("twice"); mockList.add("twice");
mockList.add("three times"); mockList.add("three times"); mockList.add("three times");
//following two verifications work exactly the same - times(1) is used by default verify(mockList).add("once"); verify(mockList,times(1)).add("once");
//exact number of invocations verification verify(mockList, times(2)).add("twice");
verify(mockList,times(3)).add("three times");
//verification using never(). never() is an alias to times(0) verify(mockList,never()).add("never happend");
//verification using atLeast()/atMost() verify(mockList,atLeastOnce()).add("three times"); verify(mockList,atLeast(2)).add("three times"); verify(mockList,atMost(5)).add("three times");

}
@Test public void testB(){ 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("zero"); //index从0开始 spy.add("one"); spy.add(2,"three"); //指定index

//prints "one" - the first element of a list System.out.println("spy.index0: "+spy.get(0));
System.out.println("spy.index1: "+spy.get(1));
System.out.println("spy.index2: "+spy.get(2));

//size() method was stubbed - 100 is printed System.out.println("Spy.size: "+ spy.size());
//optionally, you can verify verify(spy).add("zero"); verify(spy).add("one"); verify(spy).add(2,"three");
}
}

看下执行结果:

mockito静态方法_mockito中文文档_mockito

我们把验证结果修改下,看会发生什么

@Test    public void testB(){        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("zero"); //默认index从0开始 spy.add("one"); spy.add(2,"three"); //指定index

//prints "one" - the first element of a list System.out.println("spy.index0: "+spy.get(0));
System.out.println("spy.index1: "+spy.get(1));
System.out.println("spy.index2: "+spy.get(2));

//size() method was stubbed - 100 is printed System.out.println("Spy.size: "+ spy.size());
//optionally, you can verify verify(spy).add("zero"); verify(spy).add("one");        verify(spy).add(2,"two");   //修改验证结果 three -> two
}

mockito静态方法_mockito中文文档_mockito

Mockito语法相对来说还是比较简练的,学习成本并不算太高.

如果你有好的工具或者框架欢迎推荐给我, 一起学习一起进步.

总结:

测试是一门技术, 更是一门艺术.也许你今天拥有的技术, 明天就会被淘汰. 同时需要我们开拓思维和眼界, 积极拥抱变化, 学习新知识, 新方法,新技能, 计算机领域讲究的是实践, 学习更要讲究方式方法. 学习和动手一定要结合, 光看不练,犹如看武功秘籍, 是永远成不了武功大侠的.

限 时 特 惠: 本站每日持续更新海量各大内部创业教程,一年会员只需98元,全站资源免费下载 点击查看详情
站 长 微 信: lzxmw777

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注