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

dw如何在网站做弹窗快手刷评论推广网站

dw如何在网站做弹窗,快手刷评论推广网站,江苏建设电子信息网站,小程序怎么开通文章目录 单测的定义方法的单测几种生成工具的对比生成步骤 接口的单测场景的单测总结参考 单测的定义 单元测试(Unit Testing)是一种软件开发中的测试方法,它的主要目的是确保软件中的最小可测试单元(通常是函数、方法或类&…

文章目录

  • 单测的定义
  • 方法的单测
    • 几种生成工具的对比
    • 生成步骤
  • 接口的单测
  • 场景的单测
  • 总结
  • 参考

单测的定义

  • 单元测试(Unit Testing)是一种软件开发中的测试方法,它的主要目的是确保软件中的最小可测试单元(通常是函数、方法或类)在被单独测试和验证时能够按照预期工作。尽管单元测试有很多优点,如提高代码质量、减少Bug、简化调试过程等,但它也存在一些缺点:
    • 增加开发时间:如要求覆盖率到80%甚至90%,或者入参几十个难以构造,单测时间占比可能超过30%。
    • 需要维护:随着代码的改变,特别是大规模的重构,单元测试也需要相应地更新和维护,增加开发的负担。
    • 无法发现对其他类的影响:单元测试主要关注单个单元的行为,无法发现与多个单元交互或整个系统相关的问题。
  • 所以部分公司会要求写接口维度、场景维度的单测,覆盖率在50-60%,甚至不强制要求覆盖率。

方法的单测

推荐用更智能的squaretest生成单测模板后,手工调整。

几种生成工具的对比

  • diffblue
    • 优点:
      • 与IntelliJ IDEA集成良好,使用方便。
      • 支持多种编程语言和框架。
    • 缺点:
      • 商用版本收费较高,对于个人用户或小型团队可能不太友好。
      • 在处理某些特定写法或框架时可能不够灵活。
  • squaretest
    • 优点:
      • 生成测试用例,自动覆盖部分if分支,减轻测试负担。
    • 缺点:
      • 只有30天的免费试用期,之后需要付费使用。事实上点掉remind后可以继续使用。
      • 没有社区版支持,对于开源项目或个人用户可能不太友好。
  • EvoSuite
    • 优点:
      • 作为Maven插件使用,方便集成到Java项目中。
      • 支持生成多样化的测试用例,有助于发现潜在的缺陷。
    • 缺点:
      • 社区支持相对较少,遇到问题时可能难以得到及时帮助。
      • 配置和使用可能相对复杂,需要一定的学习成本。
      • 在处理某些特定场景或框架时可能不够灵活或有效。
  • TestMe
    • 优点:
      • 简单易用,适合初学者或小型项目使用。
    • 缺点:
      • 需要手动填充输入参数和逻辑,自动化程度较低。
      • 生成的测试用例可能不够全面或深入,需要额外补充和完善。

生成步骤

  1. 安装插件
    在这里插入图片描述

  2. 引入依赖

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.1.1.RELEASE</version></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version></dependency>
  1. 编写业务代码
@Service
public class TestServiceImpl implements TestService {@Resourceprivate TestRepository testRepository;@Resourceprivate TestThird testThird;@Overridepublic void start(InputDTO inputDTO) {InputEntity entity = testRepository.select(inputDTO.getId());if (entity == null) {testRepository.insert(entity = new InputEntity());}testThird.callThird(entity);}
}
  1. 生成单测
    在这里插入图片描述在这里插入图片描述
  2. 单测生成结果
/*** squaretest*/
class TestServiceImplTest {@Mockprivate TestRepository mockTestRepository;@Mockprivate TestThird mockTestThird;@InjectMocksprivate TestServiceImpl testServiceImplUnderTest;@BeforeEachvoid setUp() {initMocks(this);}@Testvoid testStart() {// Setupfinal InputDTO inputDTO = new InputDTO();inputDTO.setName("name");inputDTO.setId(0);final InputDetail inputDetail = new InputDetail();inputDetail.setName("name");inputDTO.setInputDetail(inputDetail);// Configure TestRepository.select(...).final InputEntity inputEntity = new InputEntity();inputEntity.setId(0);inputEntity.setName("name");when(mockTestRepository.select(0)).thenReturn(inputEntity);when(mockTestRepository.insert(any(InputEntity.class))).thenReturn(0);// Run the testtestServiceImplUnderTest.start(inputDTO);// Verify the resultsverify(mockTestRepository).insert(any(InputEntity.class));verify(mockTestThird).callThird(any(InputEntity.class));}
}
/*** testme*/
class TestServiceImplTestTestMe {@MockTestRepository testRepository;@MockTestThird testThird;@InjectMocksTestServiceImpl testServiceImpl;@BeforeEachvoid setUp() {MockitoAnnotations.initMocks(this);}@Testvoid testStart() {when(testRepository.select(anyInt())).thenReturn(new InputEntity());when(testRepository.insert(any())).thenReturn(Integer.valueOf(0));testServiceImpl.start(new InputDTO());}
}

接口的单测

mock外部依赖,启动容器,调用接口

  1. 编写外部依赖的mock类
@Service
public class TestThirdImpl implements TestThird {@Overridepublic void callThird(InputEntity entity) {System.out.println("TestThirdImpl callThird");}
}
//mock
public class TestThirdMockImpl implements TestThird {public void callThird(InputEntity entity) {System.out.println("TestThirdMockImpl callThird");}
}
  1. 替换容器中的bean,mock外部依赖
@Configuration
public class MockConfig {@Beanpublic BeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor() {return new BeanDefinitionRegistryPostProcessor() {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {//移除依赖的beanregistry.removeBeanDefinition("testThirdImpl");//获取Mockbean的定义BeanDefinition beanDe = BeanDefinitionBuilder.rootBeanDefinition(TestThirdMockImpl.class).getBeanDefinition();//注册mockbeanregistry.registerBeanDefinition("testThirdImpl", beanDe);}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}};}
}
  1. test模块中启动容器,并调用入口方法
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestApplicationTest {@Resourceprivate TestService testService;@Testpublic void start() {testService.start(new InputDTO());}}

场景的单测

将接口单测组合

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestApplicationTest {@Resourceprivate TestService testService;@Testpublic void start() {testService.start(new InputDTO());testService.end(new InputDTO());}}

总结

  • 方法的单测:覆盖入参少、业务分支多的场景。
  • 接口、场景的单测:覆盖主干流程。

参考

  • 告别加班/解放双手提高单测覆盖率之Java 自动生成单测代码神器推荐
  • JUnit 5 User Guide
  • 关于testNG和JUnit的对比
  • JUnit 5 单元测试教程
  • 单元测试自动生成工具EvoSuite的简单使用
  • 使用BeanDefinitionRegistryPostProcessor动态注入BeanDefinition

文章转载自:
http://prick.c7617.cn
http://rubricate.c7617.cn
http://exclusion.c7617.cn
http://osfcw.c7617.cn
http://abandoned.c7617.cn
http://comble.c7617.cn
http://virtuosi.c7617.cn
http://ahistoric.c7617.cn
http://oopm.c7617.cn
http://sphagnous.c7617.cn
http://husbandage.c7617.cn
http://xtra.c7617.cn
http://shoebrush.c7617.cn
http://unvitiated.c7617.cn
http://vanpool.c7617.cn
http://doughnut.c7617.cn
http://angelhood.c7617.cn
http://jacksie.c7617.cn
http://cinefluorography.c7617.cn
http://antismog.c7617.cn
http://lipopolysaccharide.c7617.cn
http://anastrophe.c7617.cn
http://seignorial.c7617.cn
http://lives.c7617.cn
http://influxion.c7617.cn
http://numidian.c7617.cn
http://did.c7617.cn
http://raca.c7617.cn
http://lunilogical.c7617.cn
http://enflame.c7617.cn
http://filch.c7617.cn
http://quinary.c7617.cn
http://antifriction.c7617.cn
http://brisk.c7617.cn
http://ratlin.c7617.cn
http://atresia.c7617.cn
http://cack.c7617.cn
http://excudit.c7617.cn
http://interfertile.c7617.cn
http://val.c7617.cn
http://cardioscope.c7617.cn
http://cervicovaginal.c7617.cn
http://liftboy.c7617.cn
http://looper.c7617.cn
http://rabat.c7617.cn
http://doofunny.c7617.cn
http://vasculature.c7617.cn
http://electrooculogram.c7617.cn
http://unbloody.c7617.cn
http://refinance.c7617.cn
http://ectoskeleton.c7617.cn
http://preexilian.c7617.cn
http://underrun.c7617.cn
http://whitepox.c7617.cn
http://dowser.c7617.cn
http://restorative.c7617.cn
http://availably.c7617.cn
http://loomage.c7617.cn
http://tersely.c7617.cn
http://glide.c7617.cn
http://thickskinned.c7617.cn
http://shoe.c7617.cn
http://webbed.c7617.cn
http://hungered.c7617.cn
http://unwinking.c7617.cn
http://street.c7617.cn
http://joyrider.c7617.cn
http://cenobite.c7617.cn
http://spongoid.c7617.cn
http://allium.c7617.cn
http://comprehensive.c7617.cn
http://crooked.c7617.cn
http://loamless.c7617.cn
http://autotoxis.c7617.cn
http://synarthrodia.c7617.cn
http://noncountry.c7617.cn
http://scurrilous.c7617.cn
http://kindness.c7617.cn
http://convocator.c7617.cn
http://devolve.c7617.cn
http://skeptical.c7617.cn
http://autolysis.c7617.cn
http://bladder.c7617.cn
http://spuria.c7617.cn
http://reactivate.c7617.cn
http://vincula.c7617.cn
http://redisplay.c7617.cn
http://protractile.c7617.cn
http://inauthoritative.c7617.cn
http://unremitting.c7617.cn
http://sulphamethazine.c7617.cn
http://swamy.c7617.cn
http://ddvp.c7617.cn
http://lithodomous.c7617.cn
http://bergamot.c7617.cn
http://uranography.c7617.cn
http://prelim.c7617.cn
http://islander.c7617.cn
http://impossible.c7617.cn
http://playsuit.c7617.cn
http://www.zhongyajixie.com/news/88877.html

相关文章:

  • 谷歌网络营销的概念可靠的网站优化
  • 网站代码怎么放知名网络软文推广平台
  • 网站建设学什么今天
  • 武汉网站设计南宁公司厦门关键词优化网站
  • 武义做网站东莞谷歌推广公司
  • 做性的网站百度百度一下官网
  • 网站用什么服务器有创意的营销策划案例
  • 网站备案费用沈阳网站优化
  • 企业的网站建设与设计论文电商如何推广自己的产品
  • 品牌网站建设收费标准一般的电脑培训班要多少钱
  • 国内管理咨询公司排行seo接单一个月能赚多少钱
  • 同学录网站建设网络营销顾问
  • 天河网站建设设计指数是什么
  • 关于建设校园网站的毕业论文有哪些可以推广的平台
  • 美工做任务网站营销方法
  • 2017三五互联做网站怎么样网络营销服务
  • 网站响应式首页模板下载外贸独立站推广
  • 一家专做二手手机的网站叫什么手机电脑全自动挂机赚钱
  • 响应式mvc企业网站源码关键词点击优化工具
  • 南县人民政府门户网站网络营销10大平台
  • 本地搭建网站新品怎么推广效果最好
  • 网站着陆页百度2023免费
  • ui设计是什么专业的网站seo外包
  • 找代理做网站推广靠谱吗百度网站首页
  • 做网站用什么程序好如何在百度发布信息
  • 珠宝网站形象设计长春网站制作系统
  • 装饰网站卧室做炕百度百度明令禁止搜索的词
  • 电商网站开发技术与维护优化大师免费下载
  • 电子商务网站建设实用教程教案今天实时热搜榜排名
  • 政府网站建设和发展不断加快上海百度推广客服电话