[Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith
Previously we have seen how to do Unit testing with Mockito;
import org.junit.Test; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; public class SomeBusinessMockTests { @Test
public void testFindTheGreatestFromAllData() {
DataService mockService = mock(DataService.class);
when( mockService.retieveAllData()).thenReturn(new int[] {24, 6, 15}); SomeBusinessImpl businessImpl = new SomeBusinessImpl(mockService);
int result = businessImpl.findTheGreatestFromAllData();
assertEquals(24, result);
}
}
In this post, we are going to see, using annotation from Mockito to make testing easier:
@RunWith(MockitoJUnitRunner.class)
public class SomeBusinessMockTests { @Mock
DataService mockService; @InjectMocks
SomeBusinessImpl businessImpl; @Test
public void testFindTheGreatestFromAllData() { when( mockService.retieveAllData()).thenReturn(new int[] {24, 6, 15});
assertEquals(24, businessImpl.findTheGreatestFromAllData());
}
}
We need to place @RunWIth for the Test class, otherwise it won't work.
@Mock makes it easier to create a mock class.
@InjectMock makes it easier to inject created mock into a class.
[Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith的更多相关文章
- [Mockito] Spring Unit Testing with Mockito
It is recommened to write unit testing with Mockito in Spring framework, because it is much faster w ...
- [Unit Testing] Mock a Node module's dependencies using Proxyquire
Sometimes when writing a unit test, you know that the module you're testing imports a module that yo ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...
- Unit Testing of Spring MVC Controllers: “Normal” Controllers
Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- C/C++ unit testing tools (39 found)---reference
http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...
- 简单介绍如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参数值做为函数的返回值(转)
本文将简单介绍如何使用PowerMock和Mockito来mock1. 构造函数2. 静态函数3. 枚举实现的单例4. 选择参数值做为函数的返回值5. 在调用mock出来的方法中,改变方法参数的值 一 ...
- Mock InjectMocks ( @Mock 和 @InjectMocks )区别
之前一直对这两个注解的区别不是很明白. 搜到过一篇博客园的文章举例说明了代码行为的区别.后来在stackoverflow上看到一个问答简单明了的解释了这两个注解在定义上的区别: 在此翻译记录一下: / ...
- Java Unit Testing - JUnit & TestNG
转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...
随机推荐
- 目标检测-ssd
intro: ECCV 2016 Oral arxiv: http://arxiv.org/abs/1512.02325 paper: http://www.cs.unc.edu/~wliu/pape ...
- jquery中对父节点和子节点的利用
<tr id='new_tr'> <td id="td_1">td1</td> <td id="td_2">td ...
- hdu1828 扫描线计算周长
和扫描线计算面积差不多,新加了lbd,rbd线段树来标记区间的左右两侧是否被填充(左右边界是否存在),numbd线段树统计区间有多少边 /*数据弱不用离散化,但是要处理一下坐标*/ #include& ...
- JQuery插件jqModal应用详解(十二)
JqModal 是jQuery的一个插件,用来在web浏览器中显示自定义通告,而且它为通用窗口框架奠定了基础. 1. 多模型支持 2. 支持拖拽和重定义大小 3, 支持远程加载窗口内容(ajax和if ...
- SimInfo获取(MCC, MNC, PLMN)
String NUMERIC = getSIMInfo(); protected String getSIMInfo() { TelephonyManager iPhoneManager = (Tel ...
- 练习题|网络编程-socket开发
1.什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件),C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 2.互联网协议是什么?分别介绍五层协议中每一 ...
- work工作消息队列Round-robin与Fair dispatch
一:介绍 1.模型 有两种情形,分别是轮训分发与公平分发. 2.出现的场景 考虑到simple queue中的缺点. 因为生产者发送消息后,消费者消费要花费时间,这个会造成消息的堆积. 二:Round ...
- 033 关于YARN的HA
一:准备 1.规划 namenode namenode ZKFC ZKFC journalnode journalnode jou ...
- C语言 提取double的每一位
#include<stdio.h> int main() { double x = 256.141592654; ; //整数部分 while(n) //整数部分输出 { ; n /= ; ...
- pandas学习(创建多层索引、数据重塑与轴向旋转)
pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或 ...