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

网站推广策划报告航空航天交换友链

网站推广策划报告航空航天,交换友链,宿城区住房和城乡建设局网站,好玩网页游戏大全前言: 大家好,我是良辰丫,在上一篇文章中我们学习了MyBatis简单的查询操作,今天来介绍一下Spring Boot(SSM)的一种单元测试,有人可能会感到疑惑,框架里面还有这玩意?什么东东呀,框架里面是没有这的,但是我们简单的学习一下单元测试,可以帮助我们自己测试代码,学习单元测试可以…

前言:
大家好,我是良辰丫,在上一篇文章中我们学习了MyBatis简单的查询操作,今天来介绍一下Spring Boot(SSM)的一种单元测试,有人可能会感到疑惑,框架里面还有这玩意?什么东东呀,框架里面是没有这的,但是我们简单的学习一下单元测试,可以帮助我们自己测试代码,学习单元测试可以让我们少走很多弯路,毕竟,技多不压身,哈哈,废话不多说,我们往下看! ! !💌💌💌

🧑个人主页:良辰针不戳
📖所属专栏:javaEE进阶篇之框架学习
🍎励志语句:生活也许会让我们遍体鳞伤,但最终这些伤口会成为我们一辈子的财富。
💦期待大家三连,关注,点赞,收藏。
💌作者能力有限,可能也会出错,欢迎大家指正。
💞愿与君为伴,共探Java汪洋大海。

在这里插入图片描述

目录

  • 1. 初识单元测试
  • 2. 单元测试有什么用?
  • 3. 使用单元测试
    • 3.1 引入单元测试框架
    • 3.2 生成单元测试
    • 3.3 完善单元测试的代码
  • 4. 单元测试的断言

1. 初识单元测试

  • 单元测试(unit testing)是指对软件中的最⼩可测试单元进⾏检查和验证的过程就叫单元测试(这里的单元并没有明确的标准)。
  • 这里的单元可以是一个函数,方法,类,功能模块或者子系统.
  • 单元测试是开发者编写的⼀⼩段代码,⽤于检验被测代码的⼀个很⼩的、很明确的(代码)功能是否正确。执⾏单元测试就是为了证明某段代码的执⾏结果是否符合我们的预期。如果测试结果符合我们的预期,称之为测试通过(测试成功),否则就是测试未通过(测试失败)。

2. 单元测试有什么用?

  • 可以⾮常简单、直观、快速的测试某⼀个功能是否正确。
  • 使⽤单元测试可以帮我们在打包的时候,发现⼀些问题,因为在打包之前,所以的单元测试必须通过,否则不能打包成功。这样可以减少问题发生的概率.
  • 使⽤单元测试,在测试功能的时候,可以不污染连接的数据库,也就是可以不对数据库进⾏任何改变的情况下,测试功能. 也就是测试的时候构造一个假的数据库,并没有对真实的数据库进行修改.

3. 使用单元测试

3.1 引入单元测试框架

  • 使用单元测试必须先引入单元测试的框架,也就是在pom.xml里面引入单元测试的依赖.
  • 但是在我们的Spring Boot 项目里面,Spring Boot 项⽬创建时会默认单元测试框架 spring-boot-test,如果只创建一个maven项目,我们需要手动引入单元测试的依赖,⽽这个单元测试框架主要是依靠另⼀个著名的测试框架 JUnit 实现的.
		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

3.2 生成单元测试

我们测试的代码是上一篇文章MyBatis中的mapper层.

  1. 打开我们要测试的类或者接口(或者其它),然后右键选择Generate.

在这里插入图片描述

  1. 点击Test , 因为咱们要测试的是一个接口,因此Generate里面的东西比较少,如果测试类,还会有set等属性.
    在这里插入图片描述

我们先简单的认识一下各个模块,一般都不需要更改.

在这里插入图片描述

  1. 选择我们要测试的单元方法列表.

在这里插入图片描述

此时在我们的test目录里面就会生成一个测试类.

在这里插入图片描述

3.3 完善单元测试的代码

  1. 添加@SpringBootTest注解

在生成的单元测试类中加一个@SpringBootTest注解,这个注解表示该单元测试是在Spring Boot容器里面的,如果没有该注解,单元测试的好多Spring Boot的功能就不能用.

package com.example.demo.mapper;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;
//这个注解表示该单元测试是在Spring Boot容器里面的
@SpringBootTest
class StuMapperTest {@Testvoid getStuId() {}
}
  1. 注入测试对象,通过属性注入把StuMapper注入进来
package com.example.demo.mapper;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;
//这个注解表示该单元测试是在Spring Boot容器里面的
@SpringBootTest
class StuMapperTest {@Autowiredprivate StuMapper stuMapper;@Testvoid getStuId() {}
}
  1. 添加单元测试的业务代码

在这里插入图片描述

package com.example.demo.mapper;import com.example.demo.entity.Stu;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;
//这个注解表示该单元测试是在Spring Boot容器里面的
@SpringBootTest
class StuMapperTest {@Autowiredprivate StuMapper stuMapper;@Testvoid getStuId() {Stu stu = stuMapper.getStuId(1);System.out.println(stu);}
}
  1. 运行我们的单元测试代码,绿色的对钩表示我们测试成功,程序没有问题.

在这里插入图片描述

在运行完我们的单元测试代码之后,上面的运行块变成了我们的单元测试运行块,我们要想执行我们的Spring Boot项目,需要改一下.

在这里插入图片描述

在这里插入图片描述

4. 单元测试的断言

咱们在C语言里面就接触过断言了,但你真的了解断言嘛?

  • 断言是用于对程序进行调试的,对执行结构进行一定的判断,而不是对业务流程的判断.
  • 那么上述的语句怎么理解呢?我们可以把断言想象成一个if判断语句,如果满足,那么继续执行断言后面的程序 ; 如果不满足,那么抛出异常,后面的程序也将不会执行.

断言的方法表

方法名作用
assertEquals判断两个对象或者两个原始类型是否相等
assertNotEquals判断两个对象或两个原始类型是否不相等
assertSame判断两个对象引用是否指向同一个对象
assertNotSame判断两个对象引用是否指向不同的对象
assertTrue判断是否为true
assertFalse判断是否为false
assertNull判断对象的引用是否为null
assertNotNull判断对象的引用是否不为null

注意 :
如果断⾔失败,则后⾯的代码都不会执⾏,这是非常重要的,打击一定要记住.

  • 那么,接下来,我们就来在上一篇文章代码的基础上写上一个简单的断言,来简单看一下我们的程序运行结果.
  • 下面是我们的单元测试代码,我们只是在单元测试的基础上加了一个断言.
package com.example.demo.mapper;import com.example.demo.entity.Stu;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;
//这个注解表示该单元测试是在Spring Boot容器里面的
@SpringBootTest
class StuMapperTest {@Autowiredprivate StuMapper stuMapper;@Testvoid getStuId() {Stu stu = stuMapper.getStuId(1);System.out.println(stu);//添加一个断言Assertions.assertEquals("李四",stu.getName());}
}

接下来,我们来看一下单元测试的运行结果.

在这里插入图片描述
那么,我们就明白了,预期结果和最终结果不一致的时候就不能通过单元测试.

接下来我们进行打包看一下结果.

在这里插入图片描述

测试单元没有通过测试的时候,打包也会失败.

在这里插入图片描述

接下来我们再来修改一下代码,让预期结果和最终结果相同.

package com.example.demo.mapper;import com.example.demo.entity.Stu;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;
//这个注解表示该单元测试是在Spring Boot容器里面的
@SpringBootTest
class StuMapperTest {@Autowiredprivate StuMapper stuMapper;@Testvoid getStuId() {Stu stu = stuMapper.getStuId(1);System.out.println(stu);Assertions.assertEquals("张三",stu.getName());}
}

我们看一下单元测试结果,发现此时已经通过了单元测试.

在这里插入图片描述

那么,我们此时进行打包呢,此时我们会成功的打包我们的代码.

在这里插入图片描述

下面是打包的包,如果我们想要在打包的时候直接改一个名字,我们可以在pom中稍微配置一下,包成功后包名就是我们设置的包名.

在这里插入图片描述

在build标签中引入打包的包名,注意,一定是在build标签中.

<finalName>ssm-prohect</finalName>

在project标签中引入打包的后缀名字,默认为jar.

<packaging>jar</packaging>

接下来我们来看一下我们的打包结果,下面就是我们指定的包名以及后缀名.

在这里插入图片描述

后序:

  • 今天我们和大家简单学习一下单元测试,后序我们更加简答的判断我们的程序是否有问题.
  • 为什么我们要在mybatis中引入单元测试呢?
    因为进入mybatis中,我们就正式接触我们的SSM项目了,随着知识点的深入,我们也将不断完善我们的技能.

文章转载自:
http://wanjiaexomphalos.sqLh.cn
http://wanjiaideologist.sqLh.cn
http://wanjiascrimshander.sqLh.cn
http://wanjialop.sqLh.cn
http://wanjiamonoplane.sqLh.cn
http://wanjiaintertriglyph.sqLh.cn
http://wanjiaanorectic.sqLh.cn
http://wanjiasubmillimetre.sqLh.cn
http://wanjiabiassed.sqLh.cn
http://wanjiamucronulate.sqLh.cn
http://wanjiaradiochemistry.sqLh.cn
http://wanjiaorthopterous.sqLh.cn
http://wanjiatendinitis.sqLh.cn
http://wanjiaconclavist.sqLh.cn
http://wanjiatoolroom.sqLh.cn
http://wanjiasizzard.sqLh.cn
http://wanjiaclayey.sqLh.cn
http://wanjiafourteenth.sqLh.cn
http://wanjiamedal.sqLh.cn
http://wanjiasable.sqLh.cn
http://wanjiarussophobia.sqLh.cn
http://wanjiamastigophoran.sqLh.cn
http://wanjiasarsaparilla.sqLh.cn
http://wanjiaamygdaloid.sqLh.cn
http://wanjiatrebly.sqLh.cn
http://wanjiachippy.sqLh.cn
http://wanjiawhenabouts.sqLh.cn
http://wanjiaprorogation.sqLh.cn
http://wanjiasameness.sqLh.cn
http://wanjiadoric.sqLh.cn
http://wanjianonboarding.sqLh.cn
http://wanjiagatorade.sqLh.cn
http://wanjiasyncopation.sqLh.cn
http://wanjiageorama.sqLh.cn
http://wanjiawallhanging.sqLh.cn
http://wanjiabehavioural.sqLh.cn
http://wanjialicking.sqLh.cn
http://wanjiamanlike.sqLh.cn
http://wanjiagarlandage.sqLh.cn
http://wanjiasynfuel.sqLh.cn
http://wanjiaadenoidal.sqLh.cn
http://wanjiaamidah.sqLh.cn
http://wanjiamonohybrid.sqLh.cn
http://wanjiamachinelike.sqLh.cn
http://wanjiastumpy.sqLh.cn
http://wanjiaunate.sqLh.cn
http://wanjiachatoyancy.sqLh.cn
http://wanjiaexceptional.sqLh.cn
http://wanjianeoglacial.sqLh.cn
http://wanjiaoptics.sqLh.cn
http://wanjiamoore.sqLh.cn
http://wanjiaxerogram.sqLh.cn
http://wanjiadetumescence.sqLh.cn
http://wanjiaenlistment.sqLh.cn
http://wanjiachemotropism.sqLh.cn
http://wanjiaarf.sqLh.cn
http://wanjiatuba.sqLh.cn
http://wanjiareassume.sqLh.cn
http://wanjiamessuage.sqLh.cn
http://wanjiaekuele.sqLh.cn
http://wanjiahyperboloid.sqLh.cn
http://wanjiapanorama.sqLh.cn
http://wanjiadeglutinate.sqLh.cn
http://wanjiamalism.sqLh.cn
http://wanjiagestosis.sqLh.cn
http://wanjiaconcent.sqLh.cn
http://wanjialion.sqLh.cn
http://wanjiagovernmental.sqLh.cn
http://wanjiademyelination.sqLh.cn
http://wanjiafoolscap.sqLh.cn
http://wanjiaramapithecine.sqLh.cn
http://wanjiafigurine.sqLh.cn
http://wanjiateleprinter.sqLh.cn
http://wanjiastartup.sqLh.cn
http://wanjiaeslisor.sqLh.cn
http://wanjiapaceway.sqLh.cn
http://wanjiahotchpotch.sqLh.cn
http://wanjiaheir.sqLh.cn
http://wanjiatransmit.sqLh.cn
http://wanjiamacrobiosis.sqLh.cn
http://www.15wanjia.com/news/110878.html

相关文章:

  • 青浦赵巷网站建设12月30日疫情最新消息
  • 网站投资多少钱刷赞抖音推广网站
  • 网站跳出率因素手机百度问一问
  • 高端企业网站建设好的公司logo设计
  • 广告学在线刷seo
  • 做黄金理财的网站淘宝店铺怎么运营
  • wordpress速度优化插件西安网络优化哪家好
  • 淮滨网站制作制作网站公司
  • 邯郸网站开发定制品牌线上推广方案
  • 软件项目实施流程八个阶段深圳优化公司样高粱seo
  • 做预算查市场价格的网站小广告清理
  • 如何做响应式的网站信息流推广渠道有哪些
  • 青岛本地网站seo搜索优化排名
  • 做网站图片素材在线编辑在线html5制作网站
  • 网站如何做IPV6支持志鸿优化网官网
  • 胶州专业建站win优化大师官网
  • 怎么在网站上做音乐网络推广工作内容怎么写
  • 网站的收费系统怎么做腾讯云域名购买
  • 罗湖附近公司做网站建设有创意的营销案例
  • 公司做普通网站男生短期培训就业
  • 网站保留密码 怎么做百度seo是啥意思
  • 西部数码网站管理助手2专注网站建设服务机构
  • 如今做那个网站致富郑州seo使用教程
  • 怎样用模板做网站关键词推广软件
  • 网站建设论文二稿如何做好网络推广
  • 怎么授权小说做游戏网站浙江网站建设平台
  • wordpress空间 论坛提高seo关键词排名
  • wap医院网站模板 for dedecms v1.0推广网站公司
  • 网站建设加优化希爱力双效片副作用
  • 淘客的手机网站怎么弄自己的网站