Mockito Hello World
Mockito Hello World
项目配置
repositories { jcenter() }
dependencies { testCompile "org.mockito:mockito-core:1.+" }
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"
}

Hello World程序
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";
}
}
}
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));
}
}
@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 Hello World的更多相关文章
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
- Junit mockito解耦合测试
Mock测试是单元测试的重要方法之一. 1.相关网址 官网:http://mockito.org/ 项目源码:https://github.com/mockito/mockito api:http:/ ...
- Android 单元测试(junit、mockito、robolectric)
1.运用JUnit4 进行单元测试 首先在工程的 src 文件夹内创建 test 和 test/java 文件夹. 打开工程的 build.gradle(Module:app)文件,添加JUnit4依 ...
- mockito使用心得
前提:pom引用<dependency> <groupId>junit</groupId> <artifactId>junit</artifact ...
- Mock之easymock, powermock, and mockito
easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...
- 用Mockito mock普通的方法
上面的例子是很理想化的状态,但是在实际的开发中,我们需要经常调用一些依赖特定环境的函数或者调用同事写的代码,而同事仅提供了接口.这个时候就需要利用Mockito来协助我们完成测试. 当然,你可以选择e ...
- mock测试框架Mockito
无论是敏捷开发.持续交付,还是测试驱动开发(TDD)都把单元测试作为实现的基石.随着这些先进的编程开发模式日益深入人心,单元测试如今显得越来越重要了.在敏捷开发.持续交付中要求单元测试一定要快(不能访 ...
- Mockito自定义verify参数Matcher
在TDD开发中,也许我们会遇见对一些重要的无返回值的行为测试,比如在用户的积分DB中增加用户的积分,这个行为对于我们的业务具有重要的价值,所以我们也希望能测试覆盖这部分业务价值.这个时候我们就得使用m ...
- Mockito学习资料
官网:http://mockito.org/ https://dzone.com/refcardz/mockito
随机推荐
- 计算程序总行数的Python代码
最近需要统计一下项目中代码的总行数,写了一个Python小程序,不得不说Python是多么的简洁,如果用Java写至少是现在代码的2倍. import os path="/Users/ron ...
- OpenCASCADE6.8.0 Reference Manual Serach Problem
OpenCASCADE6.8.0 Reference Manual Serach Problem eryar@163.com 1. Problem 有网友反映OpenCASCADE6.8.0的Refe ...
- WPF自定义控件与样式(1)-矢量字体图标(iconfont)
一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...
- 深入理解javascript中的富文本编辑
前面的话 一说起富文本,人们第一印象就是像使用word一样,在网页上操作文档.实际上差不多就是这样.富文本编辑,又称为WYSIWYG (What You See Is What You Get所见即所 ...
- Android调用系统相机功能
在常规应用开发过程中,我们经常会使用到手机的相机功能,通过调用系统相机方便快捷的帮助我们实现拍照功能,本篇我将带领大家实现一下,如何通过调用系统相机实现拍照. 第一种:调用系统相机拍照,通过返回的照片 ...
- 虚拟化 - 每天5分钟玩转 OpenStack(2)
OpenStack是云操作系统,要学习OpenStack,首先需要掌握一些虚拟化和云计算的相关知识. 虚拟化 虚拟化是云计算的基础.简单的说,虚拟化使得在一台物理的服务器上可以跑多台虚拟机,虚拟机共享 ...
- EasyUI Tabs绑定右键
JS: /*为选项卡绑定右键*/ $("#tabs").tabs({ onConte ...
- Windows 使用 Yeoman generators 创建 ASP.NET 应用程序
上一篇:<Windows 搭建 .NET 跨平台环境并运行应用程序> 阅读目录: Install Node.js Install yeoman-generators Create ASP. ...
- PowerPoint基础
一.基础 默认后缀ppt,pptx office2003和以后的版本只支持ppt, 可以将pptx另存为ppt97-2003 二.修改PPT尺寸 三.新建幻灯片 四.字体与段落设置 五.主题与字体 六 ...
- 译:DOM2中的高级事件处理(转)
17.2. DOM2中的高级事件处理(Advanced Event Handling with DOM Level 2) 译自:JavaScript: The Definitive Gu ...