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

广点通完善的seo网站

广点通,完善的seo网站,wordpress编辑器共存,深圳龙岗淘宝网站建设公司有哪些为鼓励单元测试,特分门别类示例各种组件的测试代码并进行解说,供开发人员参考。 本文中的测试均基于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.15wanjia.com/news/52425.html

相关文章:

  • wordpress能大网站关键词代发包收录
  • 哈尔滨网站建设效果好seo推广学院
  • b2b平台代表百度seo和sem的区别
  • 固定ip做网站路由设置网站运营包括哪些内容
  • 做图片素材的网站网站免费网站免费
  • wordpress 速卖通插件新站优化案例
  • 查学校去哪个网站韶关seo
  • python做的网站如何部署sem是什么牌子
  • 58同城深圳招聘网站企业邮箱哪个好
  • 3d建模师可以自学吗百度 seo优化作用
  • 咖啡店网站模板站长之家最新域名查询
  • 网站登录账号密码保存百度竞价优化软件
  • 大学 网站开发 专业长沙seo优化价格
  • 个人外贸网站制作百度竞价运营
  • 陕西网站建设公司找哪家搜索引擎关键词优化
  • 做商城网站要哪些流程图外链发布平台有哪些
  • 竞价安徽新站优化
  • 如何做网站泛目录解析游戏代理300元一天
  • 1核2g 做网站cpc广告接单平台
  • 淄博网站制作开发优化dw网页制作教程
  • 做网站不用数据库可以吗外链seo推广
  • 网站开发的理解宁德市地图
  • 航拍类wordpress模板网页关键词排名优化
  • 网站建设培训班百度搜索推广费用
  • 泰安建设信息网站网络推广和运营的区别
  • 自建外贸推广网站有哪些游戏代理平台
  • 淘宝客怎么样做网站武汉seo哪家好
  • 摄影瀑布流网站模板太原seo关键词优化
  • 如何规范网站使用让顾客进店的100条方法
  • 公安网站备案到底需要什么seo搜索优化是什么意思