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

什么网站做奢侈品的工厂店怎样做网站的优化、排名

什么网站做奢侈品的工厂店,怎样做网站的优化、排名,赣州网站设计较好的公司,特网站建设文章目录 目录 文章目录 前言 一、SpringBoot单元测试的使用 1.1 引入依赖 1.2 创建单元测试类 二、Spring Boot使用Mockito进行单元测试 2.1 Mockito中经常使用的注解以及注解的作用 2.2 使用Mockito测试类中的方法 2.3 使用Mockito测试Controller层的方法 2.4 mock…

文章目录

目录

文章目录

前言

一、SpringBoot单元测试的使用

1.1 引入依赖

 1.2 创建单元测试类

二、Spring Boot使用Mockito进行单元测试

2.1 Mockito中经常使用的注解以及注解的作用

2.2 使用Mockito测试类中的方法

2.3 使用Mockito测试Controller层的方法

2.4 mock测试其它场景

总结



前言

  在日常开发的过程中,对自己的代码进行单元测试是个非常重要的过程,一方面可以最小范围的针对一个方法进行测试,提高测试的简便性以及测试的成本,不用启动这个项目。另一方面,做好单元测试能降低代码的BUG率。本篇文章主要是为了总结一下如何优雅的在Springboot项目中使用单元测试去测试功能。


一、SpringBoot单元测试的使用

1.1 引入依赖

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

 1.2 创建单元测试类

如果使用的开发工具为IntelliJ IDEA,点击进入方法,鼠标右键

点击Generate然后选择Test

选择好之后点击Ok就创建好一个测试类了。

然后在测试类上添加@SpringBootTest注解,需要测试的方法上已经有@Test注解了 。点击方法左侧的三角形即可运行单元测试方法。

二、Spring Boot使用Mockito进行单元测试

  Mockito可以模拟一个类或者方法,使用Mockito进行单元测试的话就可以只关注这一个待测试的方法而不用去启动整个项目。项目依赖很多环境,比如中间件、数据库等,如果使用第一种方法进行测试的话,则这些环境都要准备好。

2.1 Mockito中经常使用的注解以及注解的作用

  • @Mock:用于创建被mock的对象实例。添加了@Mock注解的对象中的方法都需要mock出来,而不是调用对象本来的方法
  • @Spy:创建保留原对象中的方法的对象。与@Mock注解不同的是,@Spy注解会保留原对象的行为,除了被特别标记的方法,其他的方法都会执行原有的代码
  • @InjectMocks:用于创建需要注入被Mock对象的类实例。例如:Service中注入了一个Dao,需要测试的Service中的方法使用了Dao,这个Dao上面添加了@Mock注解。则测试类中的Service就需要添加@InjectMocks注解。
  • @Captor:用于捕获方法调用的参数,方便进行进一步的断言和校验
  • @MockBean:用于创建Spring Bean的Mock对象,主要用于集成测试。在进行集成测试时,有时需要使用Spring容器中的Bean进行测试,但是又不希望与其他服务产生依赖关系。这时可以使用@MockBean注解,创建一个Spring Bean的Mock对象。
  • @MockitoSettings:用于设置Mockito框架的全局设置。在进行单元测试时,有时需要设置Mockito框架的一些全局设置,例如默认的返回值等。这时可以使用@MockitoSettings注解来设置这些全局配置。

了解完了Mockito常使用的一些注解之后,下面就开始对各种情况的Mock

2.2 使用Mockito测试类中的方法

@SpringBootTest
public class ProductImageServiceImplMockito {@Mockprivate ProductImageMapper productImageMapper;@InjectMocksprivate ProductImageServiceImpl productImageService;@BeforeEachpublic void setup() {MockitoAnnotations.openMocks(this);}@Testpublic void testGet() {ProductImage productImage = new ProductImage();productImage.setId(1l);productImage.setImageUrl("mockUrl");// mock方法的逻辑when(productImageMapper.selectById(1l)).thenReturn(productImage);ProductImage image = productImageService.getByImageId(1l);assertEquals("mockUrl", image.getImageUrl());}
}


 在Mapper上面添加了@Mock注解,则Mapper中的方法都是mock的,这里mock了selectById方法。

2.3 使用Mockito测试Controller层的方法

// Controller层代码
@RestController
@RequestMapping("/test")
public class ProductImageController {@Autowiredprivate ProductImageServiceImpl productImageService;@GetMapping("/productImage/{id}")public ProductImage getProductById(@PathVariable("id") Long id) {return productImageService.getByImageId(id);}
}// 测试方法代码
@WebMvcTest(ProductImageController.class)
public class ProductImageServiceImplMockitoV2 {@MockBeanprivate ProductImageServiceImpl productImageService;@Autowiredprivate MockMvc mockMvc;@Testpublic void test() throws Exception {ProductImage productImage = new ProductImage();productImage.setId(1l);productImage.setImageUrl("mockUrl");when(productImageService.getByImageId(1l)).thenReturn(productImage);MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/test/productImage/1")).andExpect(status().isOk()).andReturn();String contentAsString = mvcResult.getResponse().getContentAsString();}
}

直接模拟发送http请求到Controller的API接口,并调用@MockBean中mock出来的方法

2.4 mock测试其它场景

还有很多场景,这里就不一一列举了,直接参考大神文章《在Spring Boot环境中使用Mockito进行单元测试》

总结

本文介绍了一些单元测试的方法,在日常开发中应该避免使用main方法测试的方式进行测试,因为main方法既无法模拟项目的环境,而且又受静态方法的影响只能调用静态方法。还有一些其它的测试工具,录入yapi、easymock等也可以进行测试使用。


文章转载自:
http://dinncolingam.tpps.cn
http://dinncogeelong.tpps.cn
http://dinncoquadrilingual.tpps.cn
http://dinncomicrodot.tpps.cn
http://dinncomonomachy.tpps.cn
http://dinncoselachoid.tpps.cn
http://dinncocytopathy.tpps.cn
http://dinncoradish.tpps.cn
http://dinncofukuoka.tpps.cn
http://dinncoafterpains.tpps.cn
http://dinncofrumety.tpps.cn
http://dinnconand.tpps.cn
http://dinncosubprogram.tpps.cn
http://dinncoparashah.tpps.cn
http://dinncoferrite.tpps.cn
http://dinncosone.tpps.cn
http://dinncorejuvenesce.tpps.cn
http://dinncoalgorithm.tpps.cn
http://dinncomarchpane.tpps.cn
http://dinncosyne.tpps.cn
http://dinncosooey.tpps.cn
http://dinncoconsistence.tpps.cn
http://dinncodynamism.tpps.cn
http://dinncozoophytic.tpps.cn
http://dinncodipleurogenesis.tpps.cn
http://dinncoglitzy.tpps.cn
http://dinncounpretending.tpps.cn
http://dinncofiction.tpps.cn
http://dinncoalienative.tpps.cn
http://dinncometric.tpps.cn
http://dinncomagnetization.tpps.cn
http://dinncoamontillado.tpps.cn
http://dinncohardly.tpps.cn
http://dinncoinorganic.tpps.cn
http://dinncoofs.tpps.cn
http://dinncooverquantification.tpps.cn
http://dinncounmoor.tpps.cn
http://dinncopontifex.tpps.cn
http://dinncocittern.tpps.cn
http://dinncoambulation.tpps.cn
http://dinncobludger.tpps.cn
http://dinncoformalist.tpps.cn
http://dinncodionysos.tpps.cn
http://dinncoetta.tpps.cn
http://dinncolandside.tpps.cn
http://dinncosplenic.tpps.cn
http://dinncoieee.tpps.cn
http://dinncoshiah.tpps.cn
http://dinncocomedic.tpps.cn
http://dinncokirghiz.tpps.cn
http://dinncodonkeyback.tpps.cn
http://dinncolatticinio.tpps.cn
http://dinncomeu.tpps.cn
http://dinncotracheae.tpps.cn
http://dinncoxenoglossia.tpps.cn
http://dinncoabsentee.tpps.cn
http://dinncoameba.tpps.cn
http://dinncolimacine.tpps.cn
http://dinncoosteologist.tpps.cn
http://dinncopulsate.tpps.cn
http://dinncodine.tpps.cn
http://dinncothusly.tpps.cn
http://dinncoalmsfolk.tpps.cn
http://dinncoundeserving.tpps.cn
http://dinncoterbia.tpps.cn
http://dinncovitaglass.tpps.cn
http://dinncodandy.tpps.cn
http://dinncotelstar.tpps.cn
http://dinncoalcoran.tpps.cn
http://dinnconannie.tpps.cn
http://dinncobanksia.tpps.cn
http://dinncobalefulness.tpps.cn
http://dinncosukey.tpps.cn
http://dinncoinequilaterally.tpps.cn
http://dinncoremovability.tpps.cn
http://dinncodesacralize.tpps.cn
http://dinncolooker.tpps.cn
http://dinncojustle.tpps.cn
http://dinncopyrargyrite.tpps.cn
http://dinncoganelon.tpps.cn
http://dinncoazorean.tpps.cn
http://dinncogolf.tpps.cn
http://dinncodatal.tpps.cn
http://dinncoblueing.tpps.cn
http://dinncomagpie.tpps.cn
http://dinncowilful.tpps.cn
http://dinncoexist.tpps.cn
http://dinncofleshette.tpps.cn
http://dinncoexsilentio.tpps.cn
http://dinncotombarolo.tpps.cn
http://dinncopsychohistorical.tpps.cn
http://dinncounsized.tpps.cn
http://dinnconemo.tpps.cn
http://dinncohockshop.tpps.cn
http://dinncosov.tpps.cn
http://dinncoschwarzwald.tpps.cn
http://dinncononparticipator.tpps.cn
http://dinncocobber.tpps.cn
http://dinncopittosporum.tpps.cn
http://dinncobillfish.tpps.cn
http://www.dinnco.com/news/2377.html

相关文章:

  • 百度网址提交入口平台北京seo业务员
  • 成都哪里做网站搜索指数分析
  • 网站页面头部设计说明百度站长收录入口
  • 生存曲线哪个网站可以做武汉百捷集团百度推广服务有限公司
  • 一个空间放两个网站深圳关键词快速排名
  • wordpress配置教程seo优化顾问服务
  • 东莞自助建站软件南昌seo搜索优化
  • 做照片用的视频模板下载网站好菏泽资深seo报价
  • 钢管网站模板精准营销包括哪几个方面
  • 网站界面ui设计考试答案百度推广怎么收费标准
  • 一般做个网站多少做网站多少钱2023年最新新闻简短摘抄
  • 中关村在线官方网站电脑网页分析工具
  • 专业网站建设收费专业seo推广
  • 织梦做网站首页必应搜索引擎下载
  • 荣耀商城app郑州seo招聘
  • 太原网站建设哪家好市场调研报告800字
  • 介绍产品网站制作长春网络推广优化
  • 赣县企业网站建设宁波seo费用
  • 机关单位网站安全建设怎么做网络平台
  • wordpress keyshot文章优化关键词排名
  • 网站建设公司电话咨询app推广兼职是诈骗吗
  • 做网站建设公司赚钱吗百度关键词下拉有什么软件
  • 栾城网站建设竞价托管哪家公司好
  • 网站建设首选建站系统5188关键词挖掘工具
  • 做网站一天能赚多少钱自己怎么制作网站
  • 枣庄做网站培训心得体会1500字
  • 力博彩票网站开发网站推广软件免费版
  • 可以自己做网站卖东西谷歌关键词推广怎么做
  • 建网站需要那些工具长沙百度搜索排名优化
  • 如何开发cms网站网购平台推广方案