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

汕头市网站建设_网站建设公司_ASP.NET_seo优化

网站建设宣传资料,永川区网站建设咨询,微网站建设报价表,做公司网站怎么删除图片为什么要建立一种预期测试失败的机制#xff1f; 有一段时间#xff0c;人们会希望并期望JUnit Test案例失败。 尽管这种情况很少见#xff0c;但确实发生了。 我需要检测JUnit测试何时失败#xff0c;然后#xff08;如果期望的话#xff09;通过而不是失败。 具体情况是… 为什么要建立一种预期测试失败的机制 有一段时间人们会希望并期望JUnit Test案例失败。 尽管这种情况很少见但确实发生了。 我需要检测JUnit测试何时失败然后如果期望的话通过而不是失败。 具体情况是我正在测试一段代码该代码可能会在对象调用内引发Assert错误。 该代码被编写为对流行的新Fest Assertions框架的增强因此为了测试功能人们可能希望测试用例会故意失败。 一个解法 一种可能的解决方案是将JUnit Rule提供的功能与注释形式的自定义标记一起使用。 为什么要使用Rule Rule对象为测试类和每个测试用例提供了类似于AOP的接口。 在运行每个测试用例之前将重置规则并且它们以Around AspectJ建议的样式公开测试用例的工作方式。 必需的代码元素 Rule对象检查每个Test用例的状态 ExpectedFailure自定义标记注释 测试用例证明代码有效 如果带注释的测试用例没有失败则抛出可选的特定异常 注意工作代码在我的github页面上可用并已添加到Maven Central。 随意分叉项目并提交拉取请求 Maven用法 dependencygroupIdcom.clickconcepts.junit/groupIdartifactIdexpected-failure/artifactIdversion0.0.9/version /dependency 用法示例 在此示例中“ exception”对象是Fest断言增强的ExpectedException请查看我的下一篇文章以展示此功能。 预期的异常将产生断言并且为了测试这些断言必须将测试用例标记为ExpectedFailure public class ExceptionAssertTest {Rulepublic ExpectedException exception ExpectedException.none();Rulepublic ExpectedTestFailureWatcher watcher ExpectedTestFailureWatcher.instance();TestExpectedFailure(The matcher should fail becasue exception is not a SimpleException)public void assertSimpleExceptionAssert_exceptionIsOfType() {// expected exception will be of type SimpleExceptionexception.instanceOf(SimpleException.class);// throw something other than SimpleException...expect failurethrow new RuntimeException(this is an exception);} } 解决方案的实施 提醒一下最新代码可在我的github页面上找到 。 规则代码ExpectedTestFailureWatcher.java import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; // YEAH Guava!! import static com.google.common.base.Strings.isNullOrEmpty;public class ExpectedTestFailureWatcher implements TestRule {/*** Static factory to an instance of this watcher** return New instance of this watcher*/public static ExpectedTestFailureWatcher instance() {return new ExpectedTestFailureWatcher();}Overridepublic Statement apply(final Statement base, final Description description) {return new Statement() {Overridepublic void evaluate() throws Throwable {boolean expectedToFail description.getAnnotation(ExpectedFailure.class) ! null;boolean failed false;try {// allow test case to executebase.evaluate();} catch (Throwable exception) {failed true;if (!expectedToFail) {throw exception; // did not expect to fail and failed...fail}}// placed outside of catchif (expectedToFail !failed) {throw new ExpectedTestFailureException(getUnFulfilledFailedMessage(description));}}/*** Extracts detailed message about why test failed* param description* return*/private String getUnFulfilledFailedMessage(Description description) {String reason null;if (description.getAnnotation(ExpectedFailure.class) ! null) {reason description.getAnnotation(ExpectedFailure.class).reason();}if (isNullOrEmpty(reason)) {reason Should have failed but didnt;}return reason;}};} } ExpectedFailure定制注释ExpectedFailure.java import java.lang.annotation.*;/*** Initially this is just a marker annotation to be used by a JUnit4 Test case in conjunction* with ExpectedTestFailure Rule to indicate that a test is supposed to be failing*/ Documented Retention(RetentionPolicy.RUNTIME) Target(value ElementType.METHOD) public interface ExpectedFailure {// TODO: enhance by adding specific information about what type of failure expected//ClassassertType() default Throwable.class;/*** Text based reason for marking test as ExpectedFailure* return String*/String reason() default ; } 自定义异常可选您可以轻松地抛出RuntimeException或现有的自定义异常 public class ExpectedTestFailureException extends Throwable {public ExpectedTestFailureException(String message) {super(message);} } 一个人不能利用预期的故障标记能力吗 强大的功能伴随着巨大的责任 建议您如果不完全了解测试失败的原因请不要将测试标记为ExpectedFailure。 建议谨慎执行此测试方法。 请勿使用ExpectedFailure注释替代Ignore 将来可能的增强可能包括指定在测试用例执行期间确定的特定断言或特定消息的方法。 已知的问题 在此当前状态下 ExpectedFailure批注可以掩盖其他声明并且在将来进行增强之前建议明智地使用此方法。 参考在Mike的站点博客上允许JUnit测试通过我们的JCG合作伙伴 Mike的失败测试案例 。 翻译自: https://www.javacodegeeks.com/2012/09/junit-pass-test-case-on-failures.html
http://www.ihoyoo.com/news/93656.html

相关文章:

  • 临沂网站定制推广型网站开发软件
  • 建收费网站新网站如何做推广
  • 网站添加新闻栏怎么做青岛网站建设推广公司哪家好
  • 网站制作是怎么做的外贸手机网站
  • 做游戏代练的网站织梦网站地图制作教程
  • 莘县网站建设一般网站建设电话
  • 扬州做网站需要多少钱pc端和移动端的网站区别是什么
  • 网站30g流量微信小程序电脑端打开
  • 有哪些网站适合大学生做兼职搜狗推广后台登录
  • 网站导航包括抚州建设工程造价管理站网站
  • 福州seo网站管理wordpress怎么做站群
  • 沈阳谷歌网站建设wordpress付费剧集网站
  • 做淘宝头像的网站有哪些有哪些做微博长图网站
  • 上海互联网网站建设公司wordpress ajax接口
  • 物流网站建设方案范文电商千万不要做虾皮
  • 肯德基网站建设为网站网站做代理
  • 深圳百度推广代理东莞企业seo推广
  • 邢台手机网站建设积玉桥网站建设
  • 200M电信宽带做网站国内快速建站
  • 福田网站建设标准数据老山网站建设
  • 百度站长工具官网wordpress设置谷歌验证
  • 网站怎么做优化排名软件编程学什么专业
  • 做亚马逊联盟一定要有网站吗js 下载服务器wordpress
  • 手机网站模板制作教程安徽农垦建设工程信息网
  • 做推广必须知道的网站吗为wordpress配置邮箱服务
  • 网站开发后怎么转安卓appseo两个域名一个网站有影响
  • 做单页网站需要做什么wordpress 轮播图插件
  • 给网站做选题wordpress 数据库建立
  • 电子商务网站建设作业文档家具网站开发报告
  • 四川做网站多少钱百科主题wordpress