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

南阳商城站北京市朝阳区

南阳商城站,北京市朝阳区,电商营销渠道有哪些,做兼职用什么网站最好junit测试springSpring 4.2附带了全新的JUnit规则#xff1a; SpringClassRule和SpringMethodRule 。 使用JUnit规则的主要优点是让开发人员摆脱SpringJUnit4ClassRunner并在Spring集成测试中利用不同的JUnit运行器。 我认为Spring JUnit Rules的最大机会是易于创建参数化的集… junit测试spring Spring 4.2附带了全新的JUnit规则 SpringClassRule和SpringMethodRule 。 使用JUnit规则的主要优点是让开发人员摆脱SpringJUnit4ClassRunner并在Spring集成测试中利用不同的JUnit运行器。 我认为Spring JUnit Rules的最大机会是易于创建参数化的集成测试。 要测试的代码 出于本文的目的我使用了现有的Spring Boot Jersey Demo应用程序 https : //github.com/kolorobot/spring-boot-jersey-demo 。 该应用程序公开了简单的REST API以与客户对象一起使用。 集成测试–“旧”方式 在Spring 4.2之前集成测试可能如下所示 RunWith(SpringJUnit4ClassRunner.class) ApplicationTest public class SaveCustomerTest {private RestTemplate restTemplate new TestRestTemplate(demo, 123);Testpublic void savesCustomer() {// actURI uri restTemplate.postForLocation(http://localhost:9000/customer,new Customer(John, Doe));// assertResponseEntityCustomer responseEntity restTemplate.getForEntity(uri, Customer.class);Customer customer responseEntity.getBody();assertThat(customer.getFirstname()).isEqualTo(John);assertThat(customer.getLastname()).isEqualTo(Doe);} } ApplicationTest是一个分组注释它包装了几个Spring的注释 Documented Inherited Retention(RetentionPolicy.RUNTIME) Target(ElementType.TYPE) SpringApplicationConfiguration(classes Application.class) WebAppConfiguration org.springframework.boot.test.IntegrationTest(server.port9000) ActiveProfiles(web) Sql(scripts classpath:data.sql, executionPhase Sql.ExecutionPhase.BEFORE_TEST_METHOD) public interface ApplicationTest {} 您可能会注意到以上测试使用标准SpringJUnit4ClassRunner 这是一个自定义运行器在JUnit集成测试中增加了Spring Framework的支持。 而且由于不能在JUnit中使用多个运行程序所以我们需要找到一种变通方法来使用Spring和JUnitParams创建参数化测试这并不是很困难的BTW。 使用Spring JUnit规则进行参数化测试 值得一提的是Spring 4.2附带了SpringJUnit4ClassRunner一个便捷替代SpringJUnit4ClassRunner Spring JUnit Rules。 让我们来看一个例子 RunWith(JUnitParamsRunner.class) ApplicationTest public class SaveCustomerParameterizedTest {ClassRulepublic static final SpringClassRule SCR new SpringClassRule();Rulepublic final SpringMethodRule springMethodRule new SpringMethodRule();private RestTemplate restTemplate new TestRestTemplate(demo, 123);TestParameterspublic void savesCustomer(String first, String last) {// actURI uri restTemplate.postForLocation(http://localhost:9000/customer,new Customer(first, last));// assertResponseEntityCustomer responseEntity restTemplate.getForEntity(uri, Customer.class);Customer customer responseEntity.getBody();assertThat(customer.getFirstname()).isEqualTo(first);assertThat(customer.getLastname()).isEqualTo(last);}public Object[] parametersForSavesCustomer() {return $($(John, Doe),$(John, Smith),$(Deborah, Johnson),$(Jan, Kowalski));} } 原始代码没有太多更改但是最重要的是 JUnitParamsRunner – JUnitParams是标准JUnit参数化测试的替代方法。 我在这里写过博客 http : //blog.codeleak.pl/2013/12/parametrized-junit-tests-with.html和此处 http : //blog.codeleak.pl/2014/11/unit-testing- excercise-with-fizzbuzz.html 。 SpringClassRule -支持的类级别功能SpringJUnit4ClassRunner 必须用相结合SpringMethodRule 。 字段的名称无关紧要但它必须是公共的静态的和最终的。 SpringMethodRule -支持的实例级和方法级设有SpringJUnit4ClassRunner因此它必须与组合SpringClassRule Parameters –测试参数的注释。 默认情况下需要parametersFormethodName方法。 使用gradle test --tests *SaveCustomerParameterizedTest运行测试将产生以下报告 如您所见执行了4个测试。 第一个花费了大部分时间因为初始化了Spring上下文后面的测试非常快。 摘要 在Spring Test Framework中添加Spring JUnit规则可以显着改善集成测试尤其是在进行参数化测试时。 但是不仅JUnitParams可以用于此目的。 您也可以尝试使用标准的JUnit org.junit.runners.Parameterized 。 资源资源 Spring框架参考-http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testcontext-junit4-rules 使用JUnitParams进行参数化的JUnit测试– http://blog.codeleak.pl/2013/12/parametrized-junit-tests-with.html JUnitParams – https://github.com/Pragmatists/JUnitParams 使用FizzBu​​zz和JUnitParams进行单元测试的练习– http://blog.codeleak.pl/2014/11/unit-testing-excercise-with-fizzbuzz.html 翻译自: https://www.javacodegeeks.com/2015/08/parameterized-integration-tests-with-spring-junit-rules.htmljunit测试spring
http://www.zqtcl.cn/news/956228/

相关文章:

  • 会员网站开发百度自己的宣传广告
  • 重庆网络推广网站推广自己设计图纸的软件
  • 国内免费的短视频素材网站什么网站做博客好
  • 个体户网站建设wordpress修改作者链接
  • 做企业网站怎么样如何做网站的登录注册
  • 网站建设中标怎么做网站文字图片
  • 济南网站推广徽hyhyk1公司展示网站模板
  • ae免费模板下载网站视频网站数据库设计
  • 找做金融的网站网站建设方面存在的问题
  • 门户网站建设与开发wordpress添加文章总数标签总数
  • 想创办一个本地的人才招聘网站_如何做市场调查问卷windows7优化大师下载
  • 做网站建设要什么证视频付费网站建设
  • html网站建设实例代码软件下载app排行榜
  • 高端个人网站网站建设密码
  • 全网seo秦皇岛市做网站优化
  • 简述站点推广有哪些方式大兴做网站公司
  • 网站关键词密度查询太仓网站设计早晨设计
  • 厦门市同安区建设局官方网站永嘉网站建设
  • 工程师网站建设网页设计与制作基础教程答案
  • php 开发手机网站建设互动平台抽手机
  • 网站 被降权网页平面设计要学什么
  • 团购网站短信平台中国建设银行网站客户注册码
  • 编辑网站的软件手机软件wordpress幻灯片源码
  • 网站开发比较厉害推荐一本学做网站的书
  • 贵州网站外包wordpress在后台修改绑定域名
  • 搜狗提交网站收录入口wordpress centos查看目录
  • 电力建设科学技术进步申报网站买机票便宜网站建设
  • 黄冈网站建设优化排名网站开发运作
  • 怎么把网站链接做二维码app跟网站的区别是什么
  • 南通住房和城乡建设局网站wordpress exif