It is recommened to write unit testing with Mockito in Spring framework, because it is much faster with Spring framework test. But in case you can doing MVC, you still need to use spring framework test.

In this post, we need to understand why to use Mockito.

For example, we have two class:

package com.example.in28minutes.cdi;

import javax.inject.Named;

@Named
public class SomeCDIDAO {
public int[] getData() {
return new int[] {5,89,100};
}
}

package com.example.in28minutes.cdi;

import javax.inject.Inject;
import javax.inject.Named; @Named
public class SomeCDIBusiness { @Inject
SomeCDIDAO someCDIDAO; public SomeCDIDAO getSomeCDIDAO() {
return someCDIDAO;
} public void setSomeCDIDAO(SomeCDIDAO someCDIDAO) {
this.someCDIDAO = someCDIDAO;
} public int findGreatest() {
int greatest = Integer.MIN_VALUE;
int[] data = someCDIDAO.getData();
for (int value : data) {
if (value > greatest) {
greatest = value;
}
}
return greatest;
}
}

'SomeCDIDAO' you can consider this class is mainly dealing with Database, fetech data, inserting data. 'SomeCDIBusiness' is using SomeCDIDAO to fetching data.

Whenever, class is dealing with Database, you should always write tests in Mockito. You should mock the opretaion, instead calling the database for real.

package com.example.in28minutes.cdi;

import com.example.in28minutes.In28minutesBasicApplication;
import com.example.in28minutes.In28minutesCdiApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import javax.inject.Inject; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; // Load the context
@RunWith(MockitoJUnitRunner.class)
public class SomeCdiBusinessTest { @InjectMocks
SomeCDIBusiness someCDIBusiness; @Mock
SomeCDIDAO mockDAO; @Test
public void testBasicScenario () {
Mockito.when(mockDAO.getData()).thenReturn(new int[] {1,2,3});
int result = someCDIBusiness.findGreatest();
assertEquals(3, result);
} @Test
public void testBasicScenario_null () {
Mockito.when(mockDAO.getData()).thenReturn(new int[] {});
int result = someCDIBusiness.findGreatest();
assertEquals(Integer.MIN_VALUE, result);
}
}

Configuration:

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency> <!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> -->

[Mockito] Spring Unit Testing with Mockito的更多相关文章

  1. [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 or ...

  2. [Spring Unit Testing] Spring Unit Testing with a Java Context

    For example, we want to test against a implemataion: package com.example.in28minutes.basic; import o ...

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

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

  4. Unit Testing of Spring MVC Controllers: Configuration

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

  5. [Spring Boot] Introduce to Mockito

    We have the implemetion: @SpringBootApplication public class MockitoDemoApplication { public static ...

  6. [Java Basics3] XML, Unit testing

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

  7. 10 Unit Testing and Automation Tools and Libraries Java Programmers Should Learn

    转自:https://javarevisited.blogspot.com/2018/01/10-unit-testing-and-integration-tools-for-java-program ...

  8. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  9. Javascript单元测试Unit Testing之QUnit

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }           QUnit是一个基于JQuery的单元测试Uni ...

随机推荐

  1. Luogu P3616 【富金森林公园】

    我们首先考虑一块石头高度变化对每个高度的查询的答案的影响, 即我们要记录,对于每个高度的查询的答案 所以要离散化高度(不然哪开的下数组啊) 不难发现,一次变化的对于不同高度的影响,对于一段连续高度是相 ...

  2. spoj227 树状数组插队序列问题

    插队问题和线段树解决的方式一样,每个结点维护值的信息是该节点之前的空位有多少,然后从后往前插点即可 注意该题要求输出的是从左往右输出每个士兵的等级,即问士兵最后排在第几个位置 /* 树状数组维护前i个 ...

  3. python 全栈开发,Day114(装饰器,排序规则,显示列,添加按钮,定制ModelForm,自定义列表页面,自定制URL)

    一.装饰器 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事务处理, ...

  4. python 全栈开发,Day16(函数第一次考试)

    考试题 Python11 期第二次考试(基础数据类型与函数部分) 考试时长:3个小时 满分:105分 一,选择题(每题2分,共24分) 1.python不支持的数据类型有 A.char B.int C ...

  5. 《剑指offer》-数组乘积,不使用除法

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]A[1]...A[i-1]A[i+1]...A[n-1].不能使用除法. ...

  6. .Net开发工程师工具箱

    Visual Studio Visual Studio Productivity Power tool:Visual Studio专业版(及以上)的扩展,具有丰富的功能,如快速查找,导航解决方案,可搜 ...

  7. HDU3306 Another kind of Fibonacci 矩阵

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3306 题意概括 A0=1,A1=1,AN=X*AN-1+Y*AN-2(N>=2).求SN,SN ...

  8. 【Java】 剑指offer(36) 二叉搜索树与双向链表

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不 ...

  9. 编辑你的数学公式——markdown中latex的使用

    前言 最近开始使用起markdown来记学习笔记,因为经常有公式要写,就需要用到latex,到网上查来查去又不太方便,而且也很少能查到写的比较全的,就准备写下这篇文章. 插入数学公式 在markdow ...

  10. <<c专家编程>>笔记

    C专家编程摘录 c操作符的优先级 有时一些c操作符有时并不会像你想象的那样工作. 下方表格将说明这个问题: 优先级问题 表达式 期望的情况 实际情况 . 优先级高于* *p.f (*p).f *(p. ...