Mockito Hello World
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.5 version = '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(),是我们要测的方法.
它会判断成员变量是否为空,不为空则调用其方法获取一些信息,否则返回一条提示信息.
-------------------
这里插播一下,关于内部类的介绍可以参见以前的一篇笔记:http://www.cnblogs.com/mengdd/archive/2013/02/08/2909307.html
内部静态类和内部非静态类可以类比:静态成员变量和非静态成员变量.
-------------------
测试类代码如下:
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.
@Test public 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: https://github.com/mockito/mockito
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了