
Mockito Hello World
发布日期:2021-05-09 05:17:35
浏览次数:8
分类:博客文章
本文共 3631 字,大约阅读时间需要 12 分钟。
Mockito Hello World
项目配置
IDE是Intellij IDEA,用gradle配置项目.
新建一个Java项目,gradle中需要有这个:
repositories { jcenter() }dependencies { testCompile "org.mockito:mockito-core:1.+" }
单元测试用JUnit 4,所以gradle文件如下:
apply plugin: 'java'apply plugin: 'idea'sourceCompatibility = 1.5version = '1.0'repositories { mavenCentral() jcenter()}dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' testCompile "org.mockito:mockito-core:1.8.5"}
写好之后在命令行执行:gradle idea
相关的包就会下载好,并出现在这里:
Hello World程序
想着写一个Hello World说明一下Mockito怎么用,结果写着写着就写复杂了.
先看代码:
package com.mengdd.examples.mockito;public class HelloWorld { private MyRobot mRobot; public MyRobot getRobot() { return mRobot; } public void setRobot(MyRobot robot) { this.mRobot = robot; } /** * This is the method we want to test. * When there is an robot, this method return the robot's information * otherwise, return some sorry text */ public String sayHello() { MyRobot robot = getRobot(); if (null != robot) { return robot.getSomeInfo(); } return "No robot here, sorry!"; } /** * MyRobot class */ public static class MyRobot { /** * Get some information from somewhere, the implementation may varies */ public String getSomeInfo() { return "Hello World -- From robot"; } }}
这段代码里面包含了一个HelloWorld类和一个内部静态类MyRobot(我的机器人).
HelloWorld中有一个方法叫sayHello(),是我们要测的方法.
它会判断成员变量是否为空,不为空则调用其方法获取一些信息,否则返回一条提示信息.
-------------------
这里插播一下,关于内部类的介绍可以参见以前的一篇笔记:
内部静态类和内部非静态类可以类比:静态成员变量和非静态成员变量.
-------------------
测试类代码如下:
package com.mengdd.examples.mockito;import org.junit.Test;import static org.hamcrest.CoreMatchers.is;import static org.junit.Assert.assertThat;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.when;public class HelloWorldTest { @Test public void testSayHelloWhenThereIsRobot() throws Exception { // We want to test the sayHello() method in the class HelloWorld. // But we don't care about how MyRobot get its information.(We can treat it as external interface). // Maybe the robot rely on many complicated things.(Although it's simple in this case.) // And when the robot's actions change in the future, we don't need to change this test case. // We can mock the MyRobot's action to make it simple and just test whether sayHello() can do its own work. // Mock steps HelloWorld.MyRobot robot = mock(HelloWorld.MyRobot.class); // Mock MyRobot class String mockInfo = "some mock info"; when(robot.getSomeInfo()).thenReturn(mockInfo); // Set behavior for mock object // real object HelloWorld helloWorld = new HelloWorld();//This is the real objec we want to test helloWorld.setRobot(robot);//set the mock robot to real object // execute the target method we want to test String result = helloWorld.sayHello(); // assert the result is what we want to have assertThat(result, is(mockInfo)); }}
因为我们要测试的是HelloWorld的sayHello()方法是否能正常工作.
我们需要假定其中robot的行为是正常的(我们并不关心robot实际上做的工作,以及它怎么做),所以这里用到了mock.
比如如果机器人的实现中要发送一个请求,我们这里就是直接mock它得到的结果,而不是真的去发这个请求.
就好像准备好了所需要的所有外部条件,看sayHello()方法的表现是否能符合我们的预期.
为了全面起见,测试了sayHello()的另一种case,这里没有用到mockito.
@Testpublic void testSayHelloWhenThereIsNoRobot() throws Exception { HelloWorld helloWorld = new HelloWorld(); helloWorld.setRobot(null); String result = helloWorld.sayHello(); assertThat(result, is("No robot here, sorry!"));}
参考资料
官网:
官方文档:
Mockito at github:
可以在这里看版本号:
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月17日 06时17分20秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux查看CUDA和cuDNN版本
2021-05-09
centos修改mysql5.7默认端口后启动异常
2021-05-09
No.017:Letter Combinations of a Phone Number
2021-05-09
RESTful API 介绍,设计
2021-05-09
C#获取Excel中所有的Sheet名称
2021-05-09
unity3d由于Camera.main.transform报空引用错误的解决方案
2021-05-09
SQL Syscolumns
2021-05-09
jQuery实现日期字符串格式化
2021-05-09
vue学习笔记(十)路由
2021-05-09
[最全整理]关于决策树的一切
2021-05-09
100天搞定机器学习|Day9-12 支持向量机
2021-05-09
100天搞定机器学习|Day19-20 加州理工学院公开课:机器学习与数据挖掘
2021-05-09
100天搞定机器学习|Day22 机器为什么能学习?
2021-05-09
100天搞定机器学习|day37 无公式理解反向传播算法之精髓
2021-05-09
数据工程师必备的8项技能,不要只知道Python!
2021-05-09
R in action读书笔记(3)-第六章:基本图形
2021-05-09
R in action读书笔记(19)第十四章 主成分和因子分析
2021-05-09
iOS UIAlertController
2021-05-09
iOS UISlider的使用
2021-05-09
iOS Xcode 打包之后,不能输出日志
2021-05-09