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

网站转换率哈尔滨建筑工程招聘信息

网站转换率,哈尔滨建筑工程招聘信息,专业的佛山网站建设公司,梧州网站建设2k9网络团队提供高品质网站建设服务java junit 异常在JUnit中#xff0c;有许多方法可以在测试代码中测试异常#xff0c;包括try-catch idiom JUnit Rule和catch-exception库。 从Java 8开始#xff0c;我们还有另一种处理异常的方法#xff1a;使用lambda表达式。 在这篇简短的博客文章中#xff0c;我将演… java junit 异常 在JUnit中有许多方法可以在测试代码中测试异常包括try-catch idiom JUnit Rule和catch-exception库。 从Java 8开始我们还有另一种处理异常的方法使用lambda表达式。 在这篇简短的博客文章中我将演示一个简单的示例说明如何利用Java 8和lambda表达式的功能来测试JUnit中的异常。 注意撰写此博客文章的动机是在catch-exception项目页面上发布的消息 Java 8的lambda表达式将使catch-exception冗余。 因此该项目将不再维护 SUT –被测系统 我们将测试以下2类抛出的异常。 第一个 class DummyService {public void someMethod() {throw new RuntimeException(Runtime exception occurred);}public void someOtherMethod() {throw new RuntimeException(Runtime exception occurred,new IllegalStateException(Illegal state));} } 第二个 class DummyService2 {public DummyService2() throws Exception {throw new Exception(Constructor exception occurred);}public DummyService2(boolean dummyParam) throws Exception {throw new Exception(Constructor exception occurred);} }所需语法 我的目标是实现与catch-exception库接近的语法 package com.github.kolorobot.exceptions.java8;import org.junit.Test; import static com.github.kolorobot.exceptions.java8.ThrowableAssertion.assertThrown;public class Java8ExceptionsTest {Testpublic void verifiesTypeAndMessage() {assertThrown(new DummyService()::someMethod) // method reference// assertions.isInstanceOf(RuntimeException.class).hasMessage(Runtime exception occurred).hasNoCause();}Testpublic void verifiesCauseType() {assertThrown(() - new DummyService().someOtherMethod(true)) // lambda expression// assertions.isInstanceOf(RuntimeException.class).hasMessage(Runtime exception occurred).hasCauseInstanceOf(IllegalStateException.class);}Testpublic void verifiesCheckedExceptionThrownByDefaultConstructor() {assertThrown(DummyService2::new) // constructor reference// assertions.isInstanceOf(Exception.class).hasMessage(Constructor exception occurred);}Testpublic void verifiesCheckedExceptionThrownConstructor() {assertThrown(() - new DummyService2(true)) // lambda expression// assertions.isInstanceOf(Exception.class).hasMessage(Constructor exception occurred);}Test(expected ExceptionNotThrownAssertionError.class) // making test passpublic void failsWhenNoExceptionIsThrown() {// expected exception not thrownassertThrown(() - System.out.println());} } 注意与catch-exception相比的优势在于我们将能够测试引发异常的构造函数。 创建“图书馆” 合成糖 assertThrown是一个静态工厂方法它创建一个ThrowableAssertion的新实例并引用了捕获的异常。 package com.github.kolorobot.exceptions.java8;public class ThrowableAssertion {public static ThrowableAssertion assertThrown(ExceptionThrower exceptionThrower) {try {exceptionThrower.throwException();} catch (Throwable caught) {return new ThrowableAssertion(caught);}throw new ExceptionNotThrownAssertionError();}// other methods omitted for now } ExceptionThrower是FunctionalInterface 可以使用lambda表达式方法引用或构造函数引用创建实例。 assertThrown接受ExceptionThrower将期望并准备处理异常。 FunctionalInterface public interface ExceptionThrower {void throwException() throws Throwable; }断言 最后我们需要创建一些断言以便可以在测试代码中验证有关teste异常的解释。 实际上 ThrowableAssertion是一种自定义断言为我们提供了一种有效地验证所捕获异常的方法。 在下面的代码中我使用了Hamcrest匹配器来创建断言。 ThrowableAssertion类的完整来源 package com.github.kolorobot.exceptions.java8;import org.hamcrest.Matchers; import org.junit.Assert;public class ThrowableAssertion {public static ThrowableAssertion assertThrown(ExceptionThrower exceptionThrower) {try {exceptionThrower.throwException();} catch (Throwable caught) {return new ThrowableAssertion(caught);}throw new ExceptionNotThrownAssertionError();}private final Throwable caught;public ThrowableAssertion(Throwable caught) {this.caught caught;}public ThrowableAssertion isInstanceOf(Class? extends Throwable exceptionClass) {Assert.assertThat(caught, Matchers.isA((ClassThrowable) exceptionClass));return this;}public ThrowableAssertion hasMessage(String expectedMessage) {Assert.assertThat(caught.getMessage(), Matchers.equalTo(expectedMessage));return this;}public ThrowableAssertion hasNoCause() {Assert.assertThat(caught.getCause(), Matchers.nullValue());return this;}public ThrowableAssertion hasCauseInstanceOf(Class? extends Throwable exceptionClass) {Assert.assertThat(caught.getCause(), Matchers.notNullValue());Assert.assertThat(caught.getCause(), Matchers.isA((ClassThrowable) exceptionClass));return this;} }AssertJ实施 如果您使用AssertJ库则可以利用org.assertj.core.api.ThrowableAssert轻松创建ThrowableAssertion AssertJ版本它提供了许多org.assertj.core.api.ThrowableAssert断言。 该类的实现比上面介绍的Hamcrest更简单。 package com.github.kolorobot.exceptions.java8;import org.assertj.core.api.Assertions; import org.assertj.core.api.ThrowableAssert;public class AssertJThrowableAssert {public static ThrowableAssert assertThrown(ExceptionThrower exceptionThrower) {try {exceptionThrower.throwException();} catch (Throwable throwable) {return Assertions.assertThat(throwable);}throw new ExceptionNotThrownAssertionError();} } AssertJ的示例测试 public class AssertJJava8ExceptionsTest {Testpublic void verifiesTypeAndMessage() {assertThrown(new DummyService()::someMethod).isInstanceOf(RuntimeException.class).hasMessage(Runtime exception occurred).hasMessageStartingWith(Runtime).hasMessageEndingWith(occurred).hasMessageContaining(exception).hasNoCause();} }摘要 仅用几行代码我们构建了非常酷的代码可帮助我们在JUnit中测试异常而无需任何其他库。 这仅仅是一个开始。 利用Java 8和lambda表达式的强大功能 资源资源 GitHub上提供了本文的源代码 请看com.github.kolorobot.exceptions.java8包 我的其他一些文章关于在JUnit中测试异常。 请看一看 定制断言 翻译自: https://www.javacodegeeks.com/2014/07/junit-testing-exception-with-java-8-and-lambda-expressions.htmljava junit 异常
http://www.huolong8.cn/news/93951/

相关文章:

  • 手机网站设置方法辽宁定制网站建设推广
  • 贵州建设厅网站备案人员查询上海突发事件
  • discuz怎么做网站地图wordpress修改主题头部图片
  • 适合代码新手做的网站网站建设推广方式
  • google属于搜索引擎类网站.wordpress页面目录
  • 成都做网站建设的公司站长之家下载
  • 贵州网站开发制作公司中国建筑工程网施工组织方案
  • 重庆的电子商务网站网站下载免费的视频软件
  • 51zwd做网站衡阳建设网站制作
  • 唯拓网站建设求职简历模板免费
  • 做海报的网站有哪些访链家网网站开发
  • wap网站开发价钱门户网站建设实施方案
  • 中化建工北京建设投资有限公司网站wordpress顶部工具栏
  • 做网站要懂哪些四库一平台怎么查建造师业绩
  • 网站的配色技巧装修设计费收费标准2020
  • 深圳网站建设服务代码购物网站那个信用好又便宜
  • 塘沽做网站比较好的wordpress apc
  • 免费检测网站seowordpress忘记管理员密码
  • ip地址做网站秦皇岛网站开发多少钱
  • 电商网站开发要哪些技术企业网站备案 淘宝客
  • 用iis制作简单网站江西星子网
  • 技术类网站模板北京制作网站多少钱
  • 福建建设信息网站php管理系统
  • 家具网站建设策划怎么重新安装我的wordpress
  • 专业教育网站建设网络广告效果评估
  • 网站建设一条可以做课后作业的网站
  • 怎么做网站后期推广织梦网站后台默认登陆路径
  • 帮客户做网站 没签合同咋办深圳建网站价格
  • 网站建设费用IP服务器镜像wordpress
  • 动易学校网站管理系统 下载为加强政协网站建设