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

宿州网站建设网站帮朋友做网站

宿州网站建设网站,帮朋友做网站,陕西网站建设的目的,南阳哪里做网站测试按照粒度可分为3层#xff1a; 单元测试#xff1a;单元测试#xff08;Unit Testing#xff09;又称为模块测试 #xff0c;是针对程序模块#xff08;软件设计的最小单位#xff09;来进行正确性检验的测试工作。程序单元是应用的最小可测试部件。在过程化编程中…测试按照粒度可分为3层 单元测试单元测试Unit Testing又称为模块测试 是针对程序模块软件设计的最小单位来进行正确性检验的测试工作。程序单元是应用的最小可测试部件。在过程化编程中一个单元就是单个程序、函数、过程等对于面向对象编程最小单元就是方法包括基类超类、抽象类、或者派生类子类中的方法。集成测试整合测试Integration Testing又称组装测试即对程序模块采用一次性或增值方式组装起来对系统的接口进行正确性检验的测试工作。整合测试一般在单元测试之后、系统测试之前进行。实践表明有时模块虽然可以单独工作但是并不能保证组装起来也可以同时工作。该测试可以由程序员或是软件品保工程师进行。端到端测试端到端测试End To End Testing又称系统测试。 通常需求开发后需要经过RD单测自测后进行提测提测往往需要达到一定的单测/自测代码覆盖率或者某些基本case通过冒烟测试符合提测要求后QA对整体功能进行端到端测试。 完善的测试流程有助于提升代码质量和研发效率这中间一方面对RD自身的业务素养有要求另一方面对团队研发流程的规范性有要求。 成熟的研发流程和体系应减少“人性”带来的不稳定性测试即是应对该不稳定性的有效方法之一。 本文记录了结合SpringBoot进行测试的一些案例示例代码参见: spring-boot-test-sample 注意区分JUnit4和JUnit5的注解本文代码基于JUnit4 {: .prompt-warning } 首先我们引入依赖 dependencies!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.mockito/groupIdartifactIdmockito-core/artifactIdscopetest/scope/dependencydependencygroupIdorg.mockito/groupIdartifactIdmockito-junit-jupiter/artifactIdscopetest/scope/dependencydependencygroupIdorg.powermock/groupIdartifactIdpowermock-module-junit4/artifactIdscopetest/scope/dependencydependencygroupIdorg.powermock/groupIdartifactIdpowermock-api-mockito2/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.6.13/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.mockito/groupIdartifactIdmockito-core/artifactIdversion2.28.2/versionscopetest/scope/dependencydependencygroupIdorg.mockito/groupIdartifactIdmockito-junit-jupiter/artifactIdversion2.28.2/versionscopetest/scope/dependencydependencygroupIdorg.powermock/groupIdartifactIdpowermock-module-junit4/artifactIdversion2.0.2/versionscopetest/scope/dependencydependencygroupIdorg.powermock/groupIdartifactIdpowermock-api-mockito2/artifactIdversion2.0.2/versionscopetest/scope/dependency/dependencies/dependencyManagement Mockito PowerMockito 单元测试 当我们仅仅需要验证代码逻辑不需要Spring的bean注入时使用Mockito PowerMockito来快速测试。 Mockito用于mock对象便于对代码逻辑进行测试验证但Mockito mock的方法有限无法mock final、private、static方法而PowerMockito框架弥补了这一点。两者可以混合使用。 案例 RunWith(PowerMockRunner.class) // mock static method PrepareOnlyThisForTest({SampleUtil.class}) PowerMockIgnore({javax.net.ssl.*,javax.management.*, javax.security.*, javax.crypto.*}) public class UnitTest {Mockprivate SampleRepository sampleRepository;InjectMocksprivate SampleService sampleService;BeforeClasspublic static void beforeAll(){System.out.print(\n\n\n\n\n\n);}AfterClasspublic static void afterAll(){System.out.print(\n\n\n\n\n\n);}Beforepublic void before(){}Afterpublic void after(){}Testpublic void getSamples() throws JSONException {PowerMockito.mockStatic(SampleUtil.class);// 注意所有when内部的方法参数必须用org.mockito.ArgumentMatchers的方法包一层不能直接传PowerMockito.when(SampleUtil.getSomething(eq(1))) // 反例.when(SampleUtil.getSomething(1)) .thenReturn(1L);PowerMockito.when(sampleRepository.selectSamples(argThat(id - id.equals(1L)))).thenReturn(new ArrayList());PowerMockito.when(sampleRepository.selectSamples(argThat(new GreaterOrEqual(1L)))).thenReturn(new ArrayList());// 这里有any(),anyString()等// 如果参数是Stringmock方法传入的是null则mock不生效传null需指定为any()Mockito.when(sampleRepository.selectSamples(any())).thenReturn(new ArrayList());// verify方法调用次数Mockito.verify(sampleRepository, Mockito.times(1)).selectSamples(any());// Mockito.verify(sampleRepository, Mockito.times(1)).selectSamples(argThat(i-i.equals(1)));// capture参数验证ArgumentCaptorLong paramCap ArgumentCaptor.forClass(Long.class);Mockito.verify(sampleRepository, Mockito.times(1)).selectSamples(paramCap.capture());Assert.assertNotNull(paramCap.getValue());ListSample samples sampleService.listSamples(1);// 如果sample.size()返回Long需要加一个 sample.size().longValue()方法Assert.assertEquals(0,samples.size());// 比较JSONJSONAssert.assertEquals({\a\:1},{\a\:1},false);// 解析JSONAssert.assertEquals(JsonPath.parse({\a\:1}).read($.a).getClass(),Integer.class);}Testpublic void mockPrivate() {try {Method method PowerMockito.method(Sample.class, privateMethodName, Long.class);method.invoke(sampleService, 0L);Assert.fail();} catch (Exception e) {Assert.assertEquals(报错信息, e.getCause().getMessage());}}} Mock和MockBean使用格式Mockito.when(localVar.method()).thenXxx… Spy和SpyBean使用格式Mockito.doXxx().when(localVar).method() Spring 测试 当依赖Spring时可以利用Spring和PowerMockito一起完成mock和test 案例 RunWith(PowerMockRunner.class) PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) PrepareOnlyThisForTest({SampleUtil.class}) ContextConfiguration(classes ControllerSliceTestWithPowerMockito.Context.class) public class ControllerSliceTestWithPowerMockito {// Import加入需要扫描的Bean// Configuration配合其他都行参考ContextConfiguration注释Import(SampleController.class)static class Context {}MockBeanprivate SampleService sampleService;SpyBeanprivate SampleConverter sampleConverter;Testpublic void zkSetup() {PowerMockito.mockStatic(SampleUtil.class);PowerMockito.when(SampleUtil.getSomething(eq(a))).thenReturn(1L);sampleConverter.test();// assert, verify}}WebMvc 切片测试 AutoConfigureWebMvc : Use this if you need to configure the web layer for testing but don’t need to use MockMvcAutoConfigureMockMvc : Use this when you just want to configure MockMvcWebMvcTest : Includes both the AutoConfigureWebMvc and the AutoConfigureMockMvc, among other functionality. 三者区别参考What’s the difference between AutoConfigureWebMvc and AutoConfigureMockMvc? 案例一 WebMvcTest(SampleController.class) RunWith(SpringRunner.class) ContextConfiguration(classes TestSampleController.TestContext.class) public class TestSampleController {private static final Logger log LoggerFactory.getLogger(TestSampleController.class);// 这里填入需要扫描的Bean这样就不用扫描整个project文件加快测试速度Import({SampleController.class, ControllerExceptionAdvice.class})Configuration // 这里兼容老版本高版本不用加static class TestContext {}Autowiredprivate MockMvc mockMvc;MockBeanprivate SampleService sampleService;// 这里用SpyBean注解当SampleController中用到了SampleConverter但是又不需要mock得用converter原本的逻辑// 或用MockBean时,在 Mockito.when(...).thenCallRealMethod()就行。SpyBeanprivate SampleConverter sampleConverter;Beforepublic void prepareMock() {// 对SampleController中调用了的SampleService的方法进行mockMockito.doNothing().when(sampleService).sampleMethod(Mockito.any());}Testpublic void shouldReturnSuccess() throws Exception {SampleRequest req new SampleRequest();req.setA(1L);String bodyJson JsonUtils.toJson(req);mockMvc.perform(MockMvcRequestBuilders.post(/test).contentType(MediaType.APPLICATION_JSON).content(bodyJson)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.content().json({\success\:true}));}Testpublic void shouldReturnErrorMsg() throws Exception {SampleRequest req new SampleRequest();req.setBString bodyJson JsonUtils.toJson(req);mockMvc.perform(MockMvcRequestBuilders.post(/test2).contentType(MediaType.APPLICATION_JSON).content(bodyJson)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.content().json({\success\:false,\errorMsg\:\错误信息\}));} }案例二 RunWith(PowerMockRunner.class) SuppressStaticInitializationFor(com.dianping.cat.Cat) // mock static method PrepareForTest({SampleUtil.class}) // spring bean PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) PowerMockIgnore({javax.net.ssl.*,javax.management.*, javax.security.*, javax.crypto.*}) // SpringBootTest从当前包向上找SpringBootConfiguration或者指定 SpringBootTest(classes SpringTestCommonConfig.class) public class SpringBeanTest {// 这个mock对象会注入Spring容器MockBeanprivate SampleRepository sampleRepository1;// 真实调用该对象逻辑SpyBeanprivate SampleRepository sampleRepository2;Autowiredprivate SampleRepository sampleRepository3;Autowiredprivate ApplicationContext applicationContext;Autowiredprivate SampleConfig sampleConfig;Testpublic void sampleBeanTest() throws JSONException {SampleRepository bean applicationContext.getBean(SampleRepository.class);Assert.assertEquals(sampleRepository1,bean);}}此外我们使用h2内存数据库达到对Mapper的测试也有testcontainers库推出用于测试与外部系统的交互这里不赘述详见示例代码
http://www.dnsts.com.cn/news/84020.html

相关文章:

  • 做网站的空间费用要多少贾汪网架公司
  • 高端人才做兼职的招聘网站有哪些简单网页模板代码
  • 如皋网站制作网站建设包含域名
  • 上海网站快速排名网站二级页面怎么做
  • 网站的建设成本广州市地图最新版 高清晰
  • 小清新文章网站教育网站如何做经营
  • 网站更新免费个人博客网站模板下载
  • 上海市住房和城乡建设厅官方网站网站竞价排名
  • 福州专业网站建设价格网络规划与设计就业前景
  • 长沙行业网站建设附近装修公司地址电话
  • 网站开发与设计课程设计北京朝阳区建设工作办公网站
  • wordpress音乐站源码云开放平台
  • 青海网站建设企业简单手工
  • 怎么用php做网站后台程序网站产品链接怎么做的
  • 洛阳网站建设培训学校上海网站制作策
  • 做网站每页面费用学校网站建设管理相关规定
  • 建设部网站 标准定额司服务器网站建设情况
  • 写资料的网站有哪些世界各国gdp排名
  • 一次备案多个网站地方社区网站 备案
  • 沈阳中小企业网站建设做logo那个网站
  • 精密模具东莞网站建设上海做什么工作最赚钱
  • 网站友情链接怎么弄贺州招聘网站建设
  • wordpress免费建站教程小程序网
  • 盐城网站建设推广优化wordpress 摘要 图片
  • wordpress多站点怎么安装主题seo研究院
  • 手机建网站优帮云教育培训的网站建设
  • 设计一个网站的价格网站建设类行业资讯
  • 设计网站策划书书画网站 建设方案
  • wordpress为什么性能差宁波seo
  • 网站建设优秀公司elementui 做的网站