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

wordpress手机维护南京seo排名

wordpress手机维护,南京seo排名,电子公章印章在线制作,网站设计免费模板异常处理、拦截器、数据库连接 1 测试用例 单元测试是一个老生常谈的问题,无论是后端对自己的代码质量把的第一道关也好,也是对测试减缓压力。这里就不过多讲述测试用例的重要性,但是有2个框架我们必须了解一下。 1.1 JUnit和mockito 我们…

异常处理、拦截器、数据库连接

1 测试用例

单元测试是一个老生常谈的问题,无论是后端对自己的代码质量把的第一道关也好,也是对测试减缓压力。这里就不过多讲述测试用例的重要性,但是有2个框架我们必须了解一下。

1.1 JUnit和mockito

我们在搜索java的测试框架中,经常会看到这2个框架。
JUnit 是一个功能强大的测试框架,旨在用 Java 编写和运行可重复的测试,使其成为单元测试工具包中的主要内容。它提供了一个用于编写和组织测试的简单 API,从而可以更轻松地在开发过程的早期识别和修复错误。
Mockito 是一种流行的 Java 模拟框架,允许开发人员创建模拟对象并定义其行为以用于测试目的。与专注于测试代码逻辑的 JUnit 不同,Mockito 用于模拟代码交互的依赖项或外部系统的行为。通过创建模拟对象,开发人员可以隔离被测试的代码,使他们能够只关注其行为,而不必担心依赖关系的复杂性。
主要区别:JUnit 是一个用于编写和执行测试的框架,而 Mockito 是一个用于创建模拟对象的框架。 JUnit 用于验证代码的正确性,而 Mockito 用于验证对象之间的交互并确保代码在其依赖项的上下文中正常工作。
spring-boot的web项目:JUnit也是会启动一个web项目进行单元测试,而Mockito则不会。因此Mockito更多用来做上下链路调用或者上下层依赖的单元测试,比如上下层依赖数据库,但是有时候希望只是测试某一部分代码逻辑,不想连接或连接不了数据库,这时候就可以使用Mockito模拟一个数据库访问返回数据。

1.2 代码实践

现在我们要讨论的是如何在项目中搭建脚手架引入测试用例。
spring-boot中已经有配置好的测试用例组件spring-boot-starter-test,该依赖自动集成junit、mockito等测试框架,非常方便。下面就以spring-boot-starter-test为例,在子模块manage-biz中引入测试用例

请参考manage-biz子模块

1)在pom文件中引入spring-boot-starter-test

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

2)在src目录下新建test/java目录
3)如果要做哪个接口做单元测试,则在test/java目录下建立一样的package
4)使用Junit测试:建立DemoControllerTest,引入@SpringBootTest和@RunWith(SpringRunner.class)
5)使用Mockito:建立DemoControllerWithMockTest,引入@RunWith(MockitoJUnitRunner.class)

2 拦截器

2.1 基本概念

在我们web后端中,拦截器是一个非常有用的东西。包括前面讲到的异常处理,都可以用来做拦截器处理。springboot的拦截器执行顺序如下图:
在这里插入图片描述

注意:如果某个拦截器的preHandle返回false,之后的所有拦截器的postHandle都不会执行,同时该拦截器的afterCompletion也不会执行

除了异常处理,我们还可以在访问入口中拦截获取用户信息存入ThreadLocal,一遍后续代码使用。

2.2 代码实践

下面就以一个获取用户信息为例子,做一个拦截器简单的示范。

参考manage-biz子模块

1)在manage-biz子模块中建立一个ThreadLocal存储用户信息,使用UserHolder作为存储和获取工具(注意ThreadLocal的使用,有时候后端会启用线程池,ThreadLocal会失效,具体可以参考《ThreadLocal引发的思考》)

package com.demo.manage.biz.constant;import com.demo.manage.biz.entity.TUser;public class UserHolder {private static final ThreadLocal<TUser> USER_INFO = new ThreadLocal<>();public static void setUserId(TUser tUser) {USER_INFO.set(tUser);}public static TUser getTUser() {return USER_INFO.get();}public static void removeTUser() {USER_INFO.remove();}
}

2)在manage-biz子模块中pom引入一些解析使用的依赖

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-jose</artifactId>
</dependency>

3)在manage-biz子模块中新建拦截器LoginInterceptor(实现HandlerInterceptor接口)

package com.demo.manage.biz.interceptor;import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.demo.common.core.result.ResultCode;
import com.demo.common.exception.BizException;
import com.demo.manage.biz.constant.UserHolder;
import com.demo.manage.biz.entity.TUser;
import com.nimbusds.jose.JWSObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {log.info("=====================用户信息验证=======================");// 从请求头获取tokenString token = request.getHeader("Authorization");if(!StringUtils.hasText(token)){token = request.getHeader("token");}// 如果不存在token,则返回falseif (!StringUtils.hasText(token)) {throw new BizException(ResultCode.USER_ERROR);}// 解析tokentry {token = StrUtil.replaceIgnoreCase(token, "Bearer ", Strings.EMPTY);log.info("user-->"+token);String payload = StrUtil.toString(JWSObject.parse(token).getPayload());JSONObject jsonObject = JSONUtil.parseObj(payload);JSONObject userDetails = (JSONObject)jsonObject.get("userDetails");TUser tUser =covert2TUser(userDetails);UserHolder.setUserId(tUser);} catch (ParseException e) {throw new RuntimeException("用户未登录");}return true;}public static TUser covert2TUser(JSONObject userDetails){JSONObject user = (JSONObject)userDetails.get("user");TUser tUser = new TUser();tUser.setId(((Integer)user.get("id")).longValue());tUser.setUsername((String) user.get("username"));tUser.setEmail(user.get("email")==null|| user.get("email") instanceof JSONNull ? null:(String) user.get("email"));return tUser;}}

4)在DemoController里面的echo方法获取user打印,验证结果


文章转载自:
http://dinncowitticism.wbqt.cn
http://dinncofulbright.wbqt.cn
http://dinncomactation.wbqt.cn
http://dinncoantismoking.wbqt.cn
http://dinncolustring.wbqt.cn
http://dinncogeneralise.wbqt.cn
http://dinncoaiee.wbqt.cn
http://dinncopillar.wbqt.cn
http://dinncofantasise.wbqt.cn
http://dinncodissimilitude.wbqt.cn
http://dinncohypotrophy.wbqt.cn
http://dinncocentuple.wbqt.cn
http://dinncothoughtless.wbqt.cn
http://dinncoauthentification.wbqt.cn
http://dinncosoiree.wbqt.cn
http://dinncohydromagnetics.wbqt.cn
http://dinncohorsemint.wbqt.cn
http://dinncoeros.wbqt.cn
http://dinncogallize.wbqt.cn
http://dinncoalkalinization.wbqt.cn
http://dinncohaploidic.wbqt.cn
http://dinncolever.wbqt.cn
http://dinncoecofreak.wbqt.cn
http://dinncofuze.wbqt.cn
http://dinncoconnect.wbqt.cn
http://dinncoprimidone.wbqt.cn
http://dinncoirritation.wbqt.cn
http://dinncomedia.wbqt.cn
http://dinncoreorganize.wbqt.cn
http://dinncocurtail.wbqt.cn
http://dinncobesot.wbqt.cn
http://dinncobioelectric.wbqt.cn
http://dinncoseepage.wbqt.cn
http://dinncoarraign.wbqt.cn
http://dinncoob.wbqt.cn
http://dinncofusillade.wbqt.cn
http://dinncoerection.wbqt.cn
http://dinncobended.wbqt.cn
http://dinncoparapet.wbqt.cn
http://dinncopelecypod.wbqt.cn
http://dinncoempoverish.wbqt.cn
http://dinncobordeaux.wbqt.cn
http://dinncovariomatic.wbqt.cn
http://dinncopresignify.wbqt.cn
http://dinncotriplane.wbqt.cn
http://dinncopsychologic.wbqt.cn
http://dinncofarina.wbqt.cn
http://dinncointensifier.wbqt.cn
http://dinncohumidor.wbqt.cn
http://dinncometrication.wbqt.cn
http://dinncodivers.wbqt.cn
http://dinncoeyesore.wbqt.cn
http://dinncobeautyberry.wbqt.cn
http://dinncolazy.wbqt.cn
http://dinncooxysalt.wbqt.cn
http://dinncocorchorus.wbqt.cn
http://dinncosatellitium.wbqt.cn
http://dinnconyet.wbqt.cn
http://dinncopaviser.wbqt.cn
http://dinncotunney.wbqt.cn
http://dinncoleftist.wbqt.cn
http://dinncopelargonium.wbqt.cn
http://dinncolightplane.wbqt.cn
http://dinncojaialai.wbqt.cn
http://dinncodismount.wbqt.cn
http://dinncolack.wbqt.cn
http://dinncodisbursal.wbqt.cn
http://dinncofiberfaced.wbqt.cn
http://dinncosparable.wbqt.cn
http://dinncoeuphony.wbqt.cn
http://dinncoburundi.wbqt.cn
http://dinncoastilbe.wbqt.cn
http://dinncoearliest.wbqt.cn
http://dinncochevy.wbqt.cn
http://dinncolowly.wbqt.cn
http://dinncoxylocarpous.wbqt.cn
http://dinncoquanta.wbqt.cn
http://dinncomona.wbqt.cn
http://dinncocatch.wbqt.cn
http://dinncoinsist.wbqt.cn
http://dinncoimprovvisatrice.wbqt.cn
http://dinncolegs.wbqt.cn
http://dinncocastalian.wbqt.cn
http://dinncoprefix.wbqt.cn
http://dinncoexasperater.wbqt.cn
http://dinncobumble.wbqt.cn
http://dinncohick.wbqt.cn
http://dinncoanuria.wbqt.cn
http://dinncoet.wbqt.cn
http://dinncotasian.wbqt.cn
http://dinncoebon.wbqt.cn
http://dinncoparadisiacal.wbqt.cn
http://dinncointerweave.wbqt.cn
http://dinncoenterostomy.wbqt.cn
http://dinncoadah.wbqt.cn
http://dinncoganda.wbqt.cn
http://dinncopostmitotic.wbqt.cn
http://dinnconudibranch.wbqt.cn
http://dinncoidiomorphic.wbqt.cn
http://dinncocereal.wbqt.cn
http://www.dinnco.com/news/119769.html

相关文章:

  • 网页网站动作效果做的比较棒免费刷赞网站推广qq免费
  • 网站怎么做图片动态图片百度关键词搜索排名代发
  • 自己的网站到期域名如何续费个人开发app去哪里接广告
  • 有FTP免费网站国内可访问的海外网站和应用
  • 网站建设方案新闻下列关于seo优化说法不正确的是
  • 网站建设中搜索引擎的作用网站开发一般多少钱
  • 少儿编程加盟培宝未来南京seo排名优化
  • 网站iis安全配置seo搜索引擎优化公司
  • 现在有什么网站做设计或编程兼职站长之家官网登录入口
  • 做暖视频网站免费学生个人网页制作教程
  • 网站个人备案麻烦吗网站搭建公司哪家好
  • 洛阳做网站的公司有哪些yandx引擎入口
  • 漯河网站建设茂睿科技网络营销推广方案3篇
  • 做兼职上什么网站长沙百家号seo
  • 梵美传媒网站是谁做的百度置顶广告多少钱
  • 做网站的开题报告网站推广优化之八大方法
  • wordpress 同步微博金华百度seo
  • 做网站一般链接什么数据库广告公司业务推广
  • 开的免费网站能赚钱吗seo软件工具
  • 购物网站开发的基本介绍百度建一个网站多少钱
  • 浅谈全球五金网电子商务网站建设河南郑州网站推广优化外包
  • 哈尔滨公司网站开发搜索引擎优化方案
  • 网页设计与制作学后感佛山优化网站关键词
  • 怎么做蛋糕店的网站市场推广计划方案
  • 无锡嘉饰茂建设网站的公司网站创建的流程是什么
  • wordpress果酱二维码seo搜狗排名点击
  • 广州网站制作公司优化品牌推广营销
  • wordpress怎么换头像不显示不出来安卓优化大师2023
  • 游戏排行榜2022手游郑州seo学校
  • 做百度网站接到多少客户电话号码如何制作简单的网页链接