当前位置: 首页 > news >正文

电商网站 支付安装百度

电商网站 支付,安装百度,网络服务费分录,山西刚刚报的病毒为鼓励单元测试,特分门别类示例各种组件的测试代码并进行解说,供开发人员参考。 本文中的测试均基于JUnit5。 单元测试实战(一)Controller 的测试 单元测试实战(二)Service 的测试 单元测试实战&#x…

为鼓励单元测试,特分门别类示例各种组件的测试代码并进行解说,供开发人员参考。

本文中的测试均基于JUnit5。

单元测试实战(一)Controller 的测试

单元测试实战(二)Service 的测试

单元测试实战(三)JPA 的测试    

单元测试实战(四)MyBatis-Plus 的测试

单元测试实战(五)普通类的测试

单元测试实战(六)其它

概述

与Controller不同,Service的测试可以脱离Spring上下文环境。这是因为Controller测试需要覆盖从HTTP请求到handler方法的路由,即需要SpringMvc的介入;而Service则是一种比较单纯的类,可以当做简单对象来测试。

我们将使用JUnit的MockitoExtension扩展来对Service对象进行测试。待测试对象为测试类的一个属性。测试仍遵循经典三段式:given、when、then;即:假设xxx……那么当yyy时……应该会zzz。

在每个测试之前应清理/重置测试数据,即操作的业务实体。

断言应主要检查Service的行为是否符合预期。

依赖

需要的依赖与Controller测试需要的依赖相同:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><scope>test</scope>
</dependency>

示例

以下是UserService的实现类UserServiceImpl。接口定义省略(从@Override注解不难推出)。

package com.aaa.api.auth.service.impl;import com.aaa.api.auth.entity.User;
import com.aaa.api.auth.repository.UserRepository;
import com.aaa.api.auth.service.UserService;
import org.springframework.stereotype.Service;import java.time.Instant;
import java.util.List;@Service
public class UserServiceImpl implements UserService {private final UserRepository repo;public UserServiceImpl(UserRepository repo) {this.repo = repo;}@Overridepublic User findById(Long id) {return repo.findById(id).orElse(null);}@Overridepublic User findByUserCode(String userCode) {return repo.findByUserCode(userCode).orElse(null);}@Overridepublic User save(User user) {user.setGmtModified(Instant.now());return repo.save(user);}@Overridepublic List<User> findAll() {return repo.findAll();}
}

以下是对UserServiceImpl进行测试的测试类:

package com.aaa.api.auth.service;import com.aaa.api.auth.entity.User;
import com.aaa.api.auth.repository.UserRepository;
import com.aaa.api.auth.service.impl.UserServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;import java.util.List;
import java.util.Optional;import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;@ExtendWith(MockitoExtension.class)
class UserServiceTest {@Mockprivate UserRepository repo;@InjectMocksprivate UserServiceImpl svc;private final User u1 = new User();private final User u2 = new User();private final User u3 = new User();@BeforeEachvoid setUp() {u1.setName("张三");u1.setUserCode("zhangsan");u1.setRole(User.ADMIN);u1.setEmail("zhangsan@aaa.net.cn");u1.setMobile("13600001234");u2.setName("李四");u2.setUserCode("lisi");u2.setRole(User.ADMIN);u2.setEmail("lisi@aaa.net.cn");u2.setMobile("13800001234");u3.setName("王五");u3.setUserCode("wangwu");u3.setRole(User.USER);u3.setEmail("wangwu@aaa.net.cn");u3.setMobile("13900001234");}@Testvoid testFindById() {// given - precondition or setupgiven(repo.findById(1L)).willReturn(Optional.of(u1));// when -  action or the behaviour that we are going testUser found = svc.findById(1L);// then - verify the outputassertThat(found).isNotNull();assertThat(found.getUserCode()).isEqualTo("zhangsan");}@Testvoid testFindByIdNegative() {// given - precondition or setupgiven(repo.findById(1L)).willReturn(Optional.empty());// when -  action or the behaviour that we are going testUser found = svc.findById(1L);// then - verify the outputassertThat(found).isNull();}@Testvoid testFindByUserCode() {// given - precondition or setupgiven(repo.findByUserCode(any())).willReturn(Optional.of(u1));// when -  action or the behaviour that we are going testUser found = svc.findByUserCode("zhangsan");// then - verify the outputassertThat(found).isNotNull();assertThat(found.getUserCode()).isEqualTo("zhangsan");}@Testvoid testFindByUserCodeNegative() {// given - precondition or setupgiven(repo.findByUserCode(any())).willReturn(Optional.empty());// when -  action or the behaviour that we are going testUser found = svc.findByUserCode("zhangsan");// then - verify the outputassertThat(found).isNull();}@Testvoid testSave() {// given - precondition or setupgiven(repo.save(any(User.class))).willAnswer((invocation -> invocation.getArguments()[0]));// when -  action or the behaviour that we are going testUser saved = svc.save(u1);// then - verify the outputassertThat(saved).isNotNull();assertThat(saved.getGmtModified()).isNotNull();}@Testvoid testSaveNegative() {// given - precondition or setupgiven(repo.save(any())).willThrow(new RuntimeException("Testing"));// when -  action or the behaviour that we are going test// User saved = svc.save(u1);// then - verify the outputassertThrows(RuntimeException.class, () -> svc.save(u1));}@Testvoid testFindAll() {// given - precondition or setupgiven(repo.findAll()).willReturn(List.of(u1, u2, u3));// when -  action or the behaviour that we are going testList<User> found = svc.findAll();// then - verify the outputassertThat(found).isNotNull();assertThat(found.size()).isEqualTo(3);}
}

测试类说明:

第22行,我们使用了JUnit的MockitoExtension扩展。

第26行,我们Mock了一个UserRepository类型的对象repo,它是待测UserServiceImpl对象的依赖。由于脱离了Spring环境,所以它是个@Mock,不是@MockBean。

接着,第29行,就是待测对象svc。它有个注解@InjectMocks,意思是为该对象进行依赖注入(Mockito提供的功能);于是,repo就被注入到svc里了。

第31-33行提供了三个测试数据,并在setUp()方法中进行初始化/重置。@BeforeEach注解使得setUp()方法在每个测试之前都会执行一遍。

接下来,从56行开始,是测试方法;每个方法都遵循given - when - then三段式。

testFindById方法是测试根据id获取User对象的。它假设repository的findById(1)会返回对象u1;那么当调用svc.findById(1)时;返回的实体就应该是u1。

testFindByIdNegative方法是根据id获取User对象的负面测试。它假设找不到ID为1的User,即repository的findById(1)会返回空;那么当调用svc.findById(1)时;返回的实体应该为空。

testFindByUserCode、testFindByUserCodeNegative与testFindById、testFindByIdNegative一样,只不过查询条件换成userCode,不再赘述。

testSave方法是测试保存User对象的。它假设repository的save()方法在保存任何User对象时都会返回该对象本身;那么当调用svc.save(u1)时;返回的实体应该为u1。注意在这里我们assert了gmtModified属性,以确认UserServiceImpl.save()方法里对该属性的设置。

testSaveNegative方法是保存User对象的负面测试。它假设repository的save()方法会抛出运行时异常;那么当调用svc.save(u1)时;会接到这个异常。

testFindAll方法是测试获取所有User对象的,它假设repository的findAll()会返回对象u1、u2、u3;那么当调用svc.findAll()时;就应返回全部三个对象。

总结

Service的测试,推荐使用@ExtendWith(MockitoExtension.class),脱离Spring上下文,使用纯Mockito打桩。其它方面,理念均与Controller测试一样。

虽然Service测试的打桩器较简单,但由于业务逻辑可能都位于这一层,需要覆盖的场景多,测试用例也应该多。Service层的测试是所有层中最重要的。

http://www.zhongyajixie.com/news/3542.html

相关文章:

  • 深圳布吉做网站百度推广一般要多少钱
  • 陕西省建设厅网站安全员报名排名函数rank怎么用
  • 怎样做网站反链网络营销专业介绍
  • 好看的网站首页特效西安网络公司
  • 吴江区建设用地申报网站教育机构排名
  • 怎样免费建企业网站吗关键词竞价排名是什么意思
  • php网站开发文本格式设置最新战争新闻事件今天
  • 网站导航栏seo优化推广工程师
  • 网站中下滑菜单怎么做seo是什么缩写
  • 贵州省住房与城乡建设部网站搜索引擎推广
  • 做网站龙岗夸克搜索入口
  • WordPress博客使用教程seo建站平台哪家好
  • 黄冈做网站公司郑州seo排名优化公司
  • 网站建设众筹女教师网课入侵录屏冫
  • 无锡网站建设无锡网络推广苏州排名搜索优化
  • 关于域名用于非网站用途的承诺书什么平台免费推广效果最好
  • 网站安装教程吉林黄页电话查询
  • 广告公司网站策划百度搜索量
  • 宜春网站建设公司哪家好不受限制的浏览器
  • 景安虚拟主机长沙企业关键词优化哪家好
  • 专门做拼团的网站国家职业技能培训学校
  • 互联网工程有限公司优化关键词排名seo软件
  • 网站不弄公安备案会怎么样全网整合营销推广
  • 怎样注册公司网站建立网页百度竞价项目
  • 海口建网站 模板seo站长查询
  • 北京建站模板厂家磁力狗在线引擎
  • 四川新闻今日头条消息网站关键词优化的价格
  • 职业生涯规划大赛主题名字手机优化大师哪个好
  • react做的网站有哪些网店代运营十大排名
  • 的网站制作一般的电脑培训班要多少钱