[Mockito] Spring Unit Testing with Mockito
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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- [Spring Boot] Introduce to Mockito
We have the implemetion: @SpringBootApplication public class MockitoDemoApplication { public static ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...
- 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 ...
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- Javascript单元测试Unit Testing之QUnit
body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; } QUnit是一个基于JQuery的单元测试Uni ...
随机推荐
- php中0,空,null和false之间区别
$a = 0; $b="0"; $c= ''; $d= null; $e = false; echo "5个变量-原始测试类型"; var_dump($a);/ ...
- python 全栈开发,Day138(scrapy框架的下载中间件,settings配置)
昨日内容拾遗 打开昨天写的DianShang项目,查看items.py class AmazonItem(scrapy.Item): name = scrapy.Field() # 商品名 price ...
- 步步为营-57-JQuery练习题
01 点谁谁哭 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- 2018-2019 2 20165203 《网络对抗技术》Exp5 MSF基础
2018-2019 2 20165203 <网络对抗技术>Exp5 MSF基础 实验内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: ...
- javaFX中解决填充(拉伸)问题
1.margin设置实现 在项目过程中,遇到此问题,如图: 如果窗口缩小,HBox(左边的包含TitledPane那部分)看不到底部 如果窗口拉大,下面就出现空白,HBox高度没拉神 办法:对包含HB ...
- MVCJSONJQuery分页实现
思路: 1.用Ado.NET获取数据 2.控制器中创建一个方法参数为搜索条件 3.返回前台一个Json对象,把对象用一个类封装 4.用JQuery接收数据 public ActionResult In ...
- 存储过程+Jquery+WebService实现三级联动:
首先看一下数据库的设计:
- 扩展BSGS算法
求解A^x ≡ B mod P (P不一定是质数)的最小非负正整数解 先放几个同余定理: 一.判断如果B==1,那么x=0,算法结束 二.若gcd(A,P)不能整除 B,则 无解,算法结束 三.若gc ...
- openstack安装-计算节点-nova计算服务安装
一.基础服务安装 先安装基础工具 yum install net-tools vim telnet wget lrzsz 1.添加hosts解析 cat << EOF >/etc/h ...
- Codeforces Round #220 (Div. 2) D - Inna and Sequence
D - Inna and Sequence 线段数维护区间有几个没有被删除的数,利用线段树的二分找第几个数在哪里,然后模拟更新就好啦. #include<bits/stdc++.h> #d ...