[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 ...
随机推荐
- Linear Algebra(未完待续)
[矩阵消元] The result of multiplying a matrix by some vector is a combination of the columns of the matr ...
- Laravel API 限速异常 HTTPException Too Many Attemps
最近线上 Laravel 错误日志发现了一个异常 HTTPException Too Many Attemps 而我一个同事在本地开发的过程中也频繁遇到这个异常.测试环境:Laravel 5.5. 网 ...
- bzoj2243树链剖分+区间合并
树链上区间合并的问题比区间修改要复杂,因为每一条重链在线段树上分布一般都是不连续的,所以在进行链上操作时要手动将其合并起来,维护两个端点值 处理时的方向问题:lca->u是一个方向,lca-&g ...
- 2018-2019 2 20165203 《网络对抗技术》 Exp1 PC平台逆向破解
2018-2019 2 20165203 <网络对抗技术> Exp1 PC平台逆向破解 实验要求 1.掌握NOP, JNE, JE, JMP, CMP汇编指令的机器码 2.掌握反汇编与十六 ...
- vim的基本用法
- Android各国语言对照表(values-xxx)
eg: 阿拉伯 Arabic SA values-ar Android各国语言对照表https://blog.csdn.net/jiangguohu1/article/details/5044014 ...
- POJ 1017 Packets【贪心】
POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 这些产品通常 ...
- hdu 2825
题解: ac自动机+dp的题目 差不多都一个套路 记录枚举了i位,匹配到自动机上的x位,然后对于匹配了哪些单词状态压缩一下就可以了 代码: #include <bits/stdc++.h> ...
- C++Primer笔记——文本查询程序(原创,未使用类)
#include <iostream> #include <vector> #include <set> #include <map> #include ...
- VB打开工程时出现不能加载MSCOMCTL.OCX
用记事本打开VBP文件找到这一行:Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.1#0; MSCOMCTL.OCX改为:Object={831FDD1 ...