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的更多相关文章

  1. [Mockito] Spring Unit Testing with Mockito

    It is recommened to write unit testing with Mockito in Spring framework, because it is much faster w ...

  2. [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 ...

  3. [Java Basics3] XML, Unit testing

    What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...

  4. Unit Testing of Spring MVC Controllers: “Normal” Controllers

    Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  5. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  6. C/C++ unit testing tools (39 found)---reference

    http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...

  7. 简单介绍如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参数值做为函数的返回值(转)

    本文将简单介绍如何使用PowerMock和Mockito来mock1. 构造函数2. 静态函数3. 枚举实现的单例4. 选择参数值做为函数的返回值5. 在调用mock出来的方法中,改变方法参数的值 一 ...

  8. Mock InjectMocks ( @Mock 和 @InjectMocks )区别

    之前一直对这两个注解的区别不是很明白. 搜到过一篇博客园的文章举例说明了代码行为的区别.后来在stackoverflow上看到一个问答简单明了的解释了这两个注解在定义上的区别: 在此翻译记录一下: / ...

  9. Java Unit Testing - JUnit & TestNG

    转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...

随机推荐

  1. Javascript面向对象基础(二)

    一: 用定义函数的方式定义类在面向对象的思想中,最核心的概念之一就是类.一个类表示了具有相似性质的一类事物的抽象,通过实例化一个类,可以获得属于该类的一个实例,即对象.在JavaScript中定义一个 ...

  2. Bitmap在Java中的应用

    一.40亿数据排序问题 给定一个最多包含40亿个随机排列的32位整数的顺序文件,找出一个不在文件中的32位整数(在文件中至少缺失这样一个数——为什么?).在具有足够内存的情况下,如何解决该问题?(编程 ...

  3. vtiger自定上传图片的字段

    废话不多说,上一篇有说到过vtiger新建模块的事 现在我新建了一个Score的模块,里面需要一个上传图片的功能 在Score界面新建一个字段叫grede 但是自定义类型里面是没有,图片这个选项的,只 ...

  4. PHPexcel的用法

    由于经常要统计学生的考试成绩,就研究了下PHPexcel这个插件 顺便说一下,读取方法只针对xls文件. 如果报错,可以先生存一个xls文件,把需要读取的xls内容复制进去. <?php //读 ...

  5. python爬取京东价格

    昨天准备爬取一个京东商品的价格,正则写好了一直是空的 后来我去页面里面看了下,价格标签里果然是空的 百度了下,大家都说是js来控制显示价格的 于是去抓包试试,找到了一条mgets的请求 中间很多参数不 ...

  6. 【CSS3】响应式布局

    准备工作1:设置Meta标签 首先我们在使用Media的时候需要先设置下面这段代码,来兼容移动设备的展示效果: 1 <meta name="viewport" content ...

  7. 【Java】 剑指offer(52) 两个链表的第一个公共结点

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入两个链表,找出它们的第一个公共结点. 思路 蛮力法:遍历第一个 ...

  8. 《Android进阶之光》--多线程编程

    No1: 实现多线程的3中方法 1)继承Thread,重写run()方法 2)实现Runnable接口,并实现该接口的run()方法 3)实现Callable接口,重写call()方法 public ...

  9. LeetCode刷题笔记--Python--28. 实现strStr()

    class Solution: def strStr(self, haystack, needle): """ :type haystack: str :type nee ...

  10. WinForm中DataGridView导出为Excel(快速版)

    public static void ExportExcel(DataGridView myDGV, string fileName) { string saveFileName = fileName ...