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. laravel使用when搜索遇到状态参数(有0的状态)的坑

    今天,使用when()方法写活动列表的接口,有两个状态搜索,都有0这个状态,当传参为0时when()就失效了... 反反复复的验证参数,传参确实是0和1啊...百思不得其解~~~后面仔细想想when( ...

  2. hdu3415 单调队列模板题

    比较裸的单调队列 先求前缀和,枚举所有结束位置1~n+k即可 #include<iostream> #include<cstdio> #include<cstring&g ...

  3. String:(字符串)中常用的方法

    package stringyiwen; //字符串中常用的方法public class StringTest03 { public static void main(String[] args) { ...

  4. ERP打印入库单(四十)

    需求描述:此购进单的基本信息,购进单位,入库单位,入库时间……此购进单批号,产品名称,生产企业,等基本信息.实现能够循环加载打印.本单金额小计,整单金额合计计算.技术需求:界面设计,循环加载数据实现函 ...

  5. bootstrap改变上传文件按钮样式,并显示已上传文件名

    参考博文: html中,文件上传时使用的<input type="file">的样式自定义 html中<input type="file"&g ...

  6. SpringMVC异常处理注解@ExceptionHandler@ControllerAdvice@ResponseStatus

    参考: http://blog.csdn.net/w372426096/article/details/78429132 http://blog.csdn.net/w372426096/article ...

  7. [OpenCV-Python] OpenCV 中视频分析 部分 VI

    部分 VI视频分析 OpenCV-Python 中文教程(搬运)目录 39 Meanshift 和 和 Camshift 目标 • 本节我们要学习使用 Meanshift 和 Camshift 算法在 ...

  8. JAVA编程:字符串转为数字求和

    程序从命令行接入数字,求和,如果接入的是字符串的解决办法 设计思想: 首先定义一个double类型的一维数组,然后在定义一个double型的变量sum,并赋值为0,用if循环判断从命令行是否有参数输入 ...

  9. Win 10 启用 Net3.5

    dism.exe /online /enable-feature /featurename:NetFX3 /Source:H:\sources\sxs

  10. OpenStack-Zun 使用

    Zun组件简介 Zun是Openstack中提供容器管理服务的组件,于2016年6月建立.Zun的目标是提供统一的Openstack API用于启动和管理容器,支持多种容器技术.Zun原来称为Higg ...