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. python+selenium一:对浏览器的操作

    # 1.打开Firefox浏览器from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://w ...

  2. 《剑指offer》-斐波那契数列

    大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 这么直接的问fibonacci,显然是迭代计算.递归的问题在于重复计算,而迭代则避免了这一点:递归是自 ...

  3. C#获取路径

    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. System.Environment.Cu ...

  4. 用Delphi从内存流中判断图片格式

    https://blog.csdn.net/my98800/article/details/53536774 废话不多说了,利用内存流来判断文件的格式,其实判断文件的前几个字节就可以简单的判断这个文件 ...

  5. 3237: [Ahoi2013]连通图 线段树分治

    题解: cf765f cf671e bzoj4184 bzoj4552 线段树分治裸题 还是介绍一下线段树分治 这个东西其实挺简单但也挺有用的 可以把删除+插入操作变成只有插入(倒着就是删除) 像这一 ...

  6. Arbitrage HDU1217

    汇率转换问题: 怎么样才能套利 可以用Floyd算法: #include<bits/stdc++.h> using namespace std; ][]; int main() { int ...

  7. python连接mysql、sqlserver、oracle、postgresql数据库的一些封装

    包括python连接数据库,以及django下配置连接数据库 # -*- coding:utf-8 -*- import psycopg2 import pymysql import pymssql ...

  8. Python库资源大全

    转载地址:https://zhuanlan.zhihu.com/p/27350980 本文是一个精心设计的Python框架.库.软件和资源列表,是一个Awesome XXX系列的资源整理,由BigQu ...

  9. RabbitMQ 初学及其深入学习推荐的一些文章

    记录一下学习RabbitMQ过程中,收获比较大的一些文章: 什么都别说,先把这6个Demo 玩一遍 https://www.rabbitmq.com/getstarted.html 大佬1号 http ...

  10. 用js来实现那些数据结构01(数组篇01-数组的增删)

    在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...