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

淄博哪家公司做网站最好软件盒子wordpress

淄博哪家公司做网站最好,软件盒子wordpress,个人网站建设教学视频,企业服务平台介绍junit:junitJUnit测试中有多种处理异常的方法。 正如我在以前的一篇文章中所写的那样 #xff0c;我最好的方法是使用org.junit.rules.ExpectedException规则。 基本上#xff0c;规则是对org.junit.Before #xff0c; org.junit.After #xff0c; org.junit.BeforeClass… junit:junit JUnit测试中有多种处理异常的方法。 正如我在以前的一篇文章中所写的那样 我最好的方法是使用org.junit.rules.ExpectedException规则。 基本上规则是对org.junit.Before org.junit.After org.junit.BeforeClass或org.junit.AfterClass注释的方法的替代或补充但它们功能更强大并且功能更多。在项目和课程之间轻松共享。 在本文中我将展示org.junit.rules.ExpectedException规则的更高级用法。 验证异常消息 标准JUnit的org.junit.Test批注提供了expected属性该属性允许您指定Throwable以在测试方法抛出指定类的异常时导致测试方法成功。 在许多情况下这似乎足够了但是如果您要验证异常消息则必须找到其他方法。 使用ExpectedException非常简单 public class ExpectedExceptionsTest {Rulepublic ExpectedException thrown ExpectedException.none();Testpublic void verifiesTypeAndMessage() {thrown.expect(RuntimeException.class);thrown.expectMessage(Runtime exception occurred);throw new RuntimeException(Runtime exception occurred);} } 在上面的代码片段中我们希望消息contains给定的子字符串。 仅与类型检查相比它更安全。 为什么 假设我们有一个ExceptionThrower 如下所示 class ExceptionsThrower {void throwRuntimeException(int i) {if (i 0) {throw new RuntimeException(Illegal argument: i must be 0);}throw new RuntimeException(Runtime exception occurred);} } 如您所见方法抛出的两个异常都是RuntimeException 因此如果我们不检查消息则不能100确定方法会抛出哪个异常。 以下测试将通过 Test public void runtimeExceptionOccurs() {thrown.expect(RuntimeException.class);// opposite to expectedexceptionsThrower.throwRuntimeException(0); }Test public void illegalArgumentExceptionOccurs() {thrown.expect(RuntimeException.class);// opposite to expectedexceptionsThrower.throwRuntimeException(1); } 检查异常中的消息将解决问题并确保您正在验证所需的异常。 与Test注释方法的expected属性相比这已经是一个优势。 但是如果您需要以更复杂的方式验证异常消息怎么办 ExpectedException通过提供Hamcrest匹配器为expectMessage方法而不是字符串允许您这样做。 让我们看下面的例子 Test public void verifiesMessageStartsWith() {thrown.expect(RuntimeException.class);thrown.expectMessage(startsWith(Illegal argument:));throw new RuntimeException(Illegal argument: i must be 0); } 如您所料您可以提供自己的匹配器以验证消息。 让我们来看一个例子。 Test public void verifiesMessageMatchesPattern() {thrown.expect(RuntimeException.class);thrown.expectMessage(new MatchesPattern([Ii]llegal .*));throw new RuntimeException(Illegal argument: i must be 0); }class MatchesPattern extends TypeSafeMatcherString {private String pattern;public MatchesPattern(String pattern) {this.pattern pattern;}Overrideprotected boolean matchesSafely(String item) {return item.matches(pattern);}Overridepublic void describeTo(Description description) {description.appendText(matches pattern ).appendValue(pattern);}Overrideprotected void describeMismatchSafely(String item, Description mismatchDescription) {mismatchDescription.appendText(does not match);} }验证异常对象 在某些情况下匹配消息可能还不够。 自定义方法可能会有例外您也想对其进行验证。 没问题 ExpectedException允许以expect方法它匹配。 Test public void verifiesCustomException() {thrown.expect(RuntimeException.class);thrown.expect(new ExceptionCodeMatches(1));throw new CustomException(1); }class CustomException extends RuntimeException {private final int code;public CustomException(int code) {this.code code;}public int getCode() {return code;} }class ExceptionCodeMatches extends TypeSafeMatcherCustomException {private int code;public ExceptionCodeMatches(int code) {this.code code;}Overrideprotected boolean matchesSafely(CustomException item) {return item.getCode() code;}Overridepublic void describeTo(Description description) {description.appendText(expects code ).appendValue(code);}Overrideprotected void describeMismatchSafely(CustomException item, Description mismatchDescription) {mismatchDescription.appendText(was ).appendValue(item.getCode());} } 请注意我同时实现了TypeSafeMatcher describeTo和describeMismatchSafely方法。 当测试失败时我需要它们产生看起来很漂亮的错误消息。 看下面的例子 java.lang.AssertionError: Expected: (an instance of java.lang.RuntimeException and expects code 1)but: expects code 1 was 2检查原因 您可以使用ExpectedException做的另一件事是验证引发异常的原因。 这也可以使用自定义匹配器完成 Test public void verifiesCauseTypeAndAMessage() {thrown.expect(RuntimeException.class);thrown.expectCause(new CauseMatcher(IllegalStateException.class, Illegal state));throw new RuntimeException(Runtime exception occurred,new IllegalStateException(Illegal state)); }private static class CauseMatcher extends TypeSafeMatcherThrowable {private final Class? extends Throwable type;private final String expectedMessage;public CauseMatcher(Class? extends Throwable type, String expectedMessage) {this.type type;this.expectedMessage expectedMessage;}Overrideprotected boolean matchesSafely(Throwable item) {return item.getClass().isAssignableFrom(type) item.getMessage().contains(expectedMessage);}Overridepublic void describeTo(Description description) {description.appendText(expects type ).appendValue(type).appendText( and a message ).appendValue(expectedMessage);} }摘要 ExpectedException规则是一个强大的功能。 通过添加Hamcrest匹配器您可以轻松地为异常测试创建健壮且可重用的代码。 可以在GitHub上找到代码示例。 还请检查我以前的文章 JUnit中处理异常的3种方法。 选择哪一个 参考 JUnit ExpectedException规则 Codeleak.pl博客上的JCG合作伙伴 Rafal Borowiec的基本知识 除外 。 翻译自: https://www.javacodegeeks.com/2014/03/junit-expectedexception-rule-beyond-basics.htmljunit:junit
http://www.huolong8.cn/news/363153/

相关文章:

  • 做网站原型的软件互联网信息服务 网站备案
  • 网站建设 宣传网站软文营销
  • 中文网站模板 免费合肥市蜀山区做个网站多少钱
  • 背投广告典型网站什么网站可以做设计
  • 小型网站怎样优化威海城乡建设局网站首页
  • 南美洲网站后缀邯郸北京网站建设
  • 找公司做网站多少钱成都个人建网站教程
  • 怎样建设网站流程下班后赚钱的100个副业
  • 菠菜彩票网站怎么建设当前最好用的wordpress主题
  • 本地上海集团网站建设商务网站创建流程是什么
  • 网站建设名片江西赣州网站建设
  • 查icp备案是什么网站网站建设文书
  • 高密做网站亚洲长尾关键词挖掘
  • 北京网站开发公司排名长治做百度网站一年多少钱
  • 服装网站模板免费下载扬州百度seo公司
  • 内容网站全案策划
  • 网站活动页面公司和公司网站的关系
  • php做网站登陆验证wordpress论坛样式
  • 品牌型网站建设特点手机网站开发+手机模拟器
  • 网站开发代码实例网站开发平台介绍
  • 2018做网站的视频网页制作与网站建设答案
  • 网站怎么 备案在自己的网站上做查分系统
  • 做疏通什么网站推广好响应式
  • 邢台网站建设邢台网站做授权登录
  • 做网站为什么要用php框架企业电话号码大全
  • 网站制作算是什么专业大型网站开发费用
  • 最专业企业营销型网站建设安徽建设工程信息网文件
  • 做家教网站的资源是什么淘宝网的网站建设
  • 江门站官网wordpress 显示二级分类
  • 中国空间站最新消息新闻php网站开发 知乎