写在前面
三流的团队做项目,二流的团队做产品,一流的团队做标准
许多技术团队为了敏捷开发,很少关注代码质量、代码稳定性与健壮性,由此导致的后果就是系统上线后,bug频发,引起用户的反感,从而造成商业价值的流失。
为了解决这一现象,我们需要成熟的单元测试工具,测试流程,开发规范等去约束自己。
so~小强书写✍️以下demo,用来引导大家使用规范化的单元测试
技术栈
- springboot
- junit
- mockito
核心包
spring-boot-starter-test
项目依赖
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${}</version>
</dependency>
项目示例
单元测试目标为()
源码示例
package com.winnie.biz.store.service.impl;
import com.winnie.biz.store.dao.IStoreDao;
import com.winnie.biz.store.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @desc 仓库服务
* @auther winnie
* @date 2021/7/2
*/
@Service
public class StoreServiceImpl implements IStoreService {
@Autowired
IStoreDao storeDao;
/**
* 剩余商品数量
* @param goodsId
* @return
*/
@Override
public Integer countLeftGoods(String goodsId) {
Integer leftGoods = storeDao.countLeftGoods(goodsId);
return leftGoods;
}
}
JUnit做单元测试示例
package com.winnie.biz.store.service.impl;
import com.winnie.biz.store.service.IStoreService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 启动springboot环境进行单元测试
* 此方式会调用真实依赖对象
*
* 单元测试开发中,应禁止此测试方式
* 理由:SpringBootTest会启动springboot应用程序
* 1. 占用系统端口
* 2. 加载非单元测试相关bean,效率低
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class StoreServiceImplTest {
@Autowired
IStoreService storeService;
@Test
public void testCountLeftGoods() {
String goodsId = "1";
Assert.assertEquals(Integer.valueOf(1), storeService.countLeftGoods(goodsId));
}
}
现象:使用SpringBootTest启动真实环境后,spring容器会注入真实对象,并且查询真实数据做断言判断。这里有两个问题:
- 启动springboot程序,会占用系统端口,若该端口正在运行,则单元测试终止
- 启动springboot程序时,系统会加载所有bean对象,付出的时间成本太高,浪费资源
结论:禁用此方式
Junit+Mockito做单元测试
package com.winnie.biz.store.service.impl;
import com.winnie.biz.store.dao.IStoreDao;
import org.junit.Assert;
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;
/**
* 以mockito环境启动
*/
@RunWith(MockitoJUnitRunner.class)
public class StoreServiceImplMockitoTest {
/**
* InjectMocks
* 注入storeService对象到上下文环境中
* <p>
* 注意,与Autowire不同的是,InjectMocks可以跟Mock注解搭配使用
*/
@InjectMocks
StoreServiceImpl storeService;
/**
* 创建mock对象,自动被注入到被@InjectMocks注解所修饰的对象里
*/
@Mock
IStoreDao storeDao;
@Test
public void testCountLeftGoods() {
String goodsId = "1";
// 方法打桩
// 若调用了方法,则不管传入任意String值,都会返回整形1
Mockito.when(storeDao.countLeftGoods(Mockito.anyString())).thenReturn(1);
Assert.assertEquals(Integer.valueOf(1), storeService.countLeftGoods(goodsId));
}
}
mock(模拟),即为模拟对象。
分析:使用mockito框架后,被mock所模拟,我们设定当调用了方法,则不管传入任意String值,都会返回整形1。因此,我们只需要关注单元测试本身的代码逻辑,与不用关心它所依赖的对象以及其真实值。
结论:推荐使用!!!推荐使用!!!推荐使用!!!
写在最后
附上本人项目地址,互相学习springboot-junit-mockito-demo
如有疑惑,请留下您宝贵的意见或建议