[Mockito] Spring Unit Testing with Mockito
Spring with Testing unit Mockito
2023-09-14 08:59:14 时间
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> -->
相关文章
- spring相关注解_redis和session区别
- Spring学习笔记(三)——Spring注解开发&spring与Junit整合测试
- spring注解有哪些_Spring 注解
- spring官方文档 中文_Spring软件
- Spring boot串口通信,windows、linux,docker环境配置说明
- 译:基于Spring Cloud Stream构建和测试 message-driven 微服务
- Spring Boot的依赖管理和构建工具
- Spring Cloud Sleuth的MDC集成实现自定义跟踪
- Spring Security 的 Filter 链和 Filter 顺序(二)
- Spring Security用户认证和授权(一)
- Spring Batch 配置方式-Java配置
- 使用docker发布spring cloud应用详解架构师
- 重写spring读取配置文件,读取加密配置文件详解编程语言
- spring 事务传播行为实例分析详解编程语言
- Spring Boot2.0之多数据源事务管理详解编程语言
- Spring Boot(十三):spring boot小技巧详解编程语言
- Spring Boot(五):spring data jpa的使用详解编程语言
- spring boot 发布 jar 包转为 war详解编程语言
- spring使用Spring整合Redis和Jedis构建高性能应用(redisjedis与)
- 为什么说Java程序员到了必须掌握 Spring Boot 的时候?
- Oracle 中的 WITH 语句使用技巧(oracle with用法)
- 分析学习Oracle深入理解使用With 分析(oracle使用with)