pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<legend id='POc35'><style id='POc35'><dir id='POc35'><q id='POc35'></q></dir></style></legend>
    <bdo id='POc35'></bdo><ul id='POc35'></ul>
  • <tfoot id='POc35'></tfoot>

    1. <i id='POc35'><tr id='POc35'><dt id='POc35'><q id='POc35'><span id='POc35'><b id='POc35'><form id='POc35'><ins id='POc35'></ins><ul id='POc35'></ul><sub id='POc35'></sub></form><legend id='POc35'></legend><bdo id='POc35'><pre id='POc35'><center id='POc35'></center></pre></bdo></b><th id='POc35'></th></span></q></dt></tr></i><div class="rzppdv7" id='POc35'><tfoot id='POc35'></tfoot><dl id='POc35'><fieldset id='POc35'></fieldset></dl></div>

      <small id='POc35'></small><noframes id='POc35'>

      1. 異常Junit 4.10后的Mockito驗證

        Mockito verify after exception Junit 4.10(異常Junit 4.10后的Mockito驗證)
          <tfoot id='RnMWa'></tfoot>
              <i id='RnMWa'><tr id='RnMWa'><dt id='RnMWa'><q id='RnMWa'><span id='RnMWa'><b id='RnMWa'><form id='RnMWa'><ins id='RnMWa'></ins><ul id='RnMWa'></ul><sub id='RnMWa'></sub></form><legend id='RnMWa'></legend><bdo id='RnMWa'><pre id='RnMWa'><center id='RnMWa'></center></pre></bdo></b><th id='RnMWa'></th></span></q></dt></tr></i><div class="brhrdbx" id='RnMWa'><tfoot id='RnMWa'></tfoot><dl id='RnMWa'><fieldset id='RnMWa'></fieldset></dl></div>
                <bdo id='RnMWa'></bdo><ul id='RnMWa'></ul>

                    <tbody id='RnMWa'></tbody>

                  <small id='RnMWa'></small><noframes id='RnMWa'>

                  <legend id='RnMWa'><style id='RnMWa'><dir id='RnMWa'><q id='RnMWa'></q></dir></style></legend>
                  本文介紹了異常Junit 4.10后的Mockito驗證的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在測試一個帶有預期異常的方法.我還需要驗證在拋出異常后(在模擬對象上)調用了一些清理代碼,但看起來驗證被忽略了.這是代碼.我正在使用 Junit ExpectedException Rule 來驗證預期的異常.

                  I am testing a method with an expected exception. I also need to verify that some cleanup code was called (on a mocked object) after the exception is thrown, but it looks like that verification is being ignored. Here is the code. I am using the Junit ExpectedException Rule to verify the expected exception.

                  @Rule
                  public ExpectedException expectedEx = ExpectedException.none();
                  
                  @Test
                  public void testExpectedException()
                  {
                     MockedObject mockObj = mock(MockedObj.class);
                     MySubject subject = new MySubject(mockedObj);
                     expectedEx.expect(MyException.class);
                     expectedEx.expectMessage("My exception message.");
                     subject.someMethodThrowingException();
                     verify(mockObj).
                         someCleanup(eq(...));
                  }
                  

                  似乎 verify 完全被忽略了.無論我在 verify 中輸入什么方法,我的測試都通過了,這不是我想要的.

                  It seems like the verify is totally being ignored. No matter what method I put in the verify, my test is passing, which is not what I want.

                  知道為什么會這樣嗎?

                  推薦答案

                  ExpectedException 由 通過 github.com/junit-team/junit/blob/master/src/main/java/org/junit/rules/TestRule.java" rel="noreferrer">JUnit @Rule.當您的代碼拋出異常時,它會向上堆棧到最近的 try/catch,這恰好位于 ExpectedException 實例中(它會檢查它是否是您期望的異常).

                  ExpectedException works by wrapping your entire test method in a try-catch block via a JUnit @Rule. When your code throws an exception, it goes up the stack to the nearest try/catch, which happens to be in the ExpectedException instance (which checks that it is the exception you're expecting).

                  在 Java 中,如果方法中發生未捕獲的異常,則控制權將永遠不會返回到該方法中稍后的語句.此處適用相同的規則:在異常發生后,控制永遠不會返回到測試中的語句.

                  In Java, if an uncaught exception occurs in a method, control will never return to statements later in that method. The same rules apply here: Control never returns to the statements in your test after the exception.

                  從技術上講,您可以將驗證放在 finally 塊中,但這往往是 一個壞習慣.您的被測系統可能會拋出意外異常,或者根本沒有異常,這將為您提供有用的失敗消息和跟蹤;但是,如果該失敗導致您的驗證或斷言在 finally 塊中失敗,那么 Java 將顯示這一點,而不是有關意外異?;蛞馔獬晒Φ南?這會使調試變得困難,尤其是因為您的錯誤將來自錯誤根本原因之后的代碼行,錯誤地暗示上面的代碼已成功.

                  Technically, you could put the verifications in a finally block, but that tends to be a bad habit. Your system-under-test might throw an unexpected exception, or no exception at all, which would give you a helpful failure message and trace; however, if that failure then causes your verifications or assertions to fail in the finally block, then Java will show that rather than a message about the unexpected exception or unexpected success. This can make debugging difficult, especially because your error will come from lines of code following the root cause of the error, incorrectly implying that the code above it succeeded.

                  如果您確實需要在異常發生后驗證狀態,則可以基于每個方法,隨時恢復到這個習慣用法:

                  If you really need to verify state after the exception, on a per-method basis, you can always revert back to this idiom:

                  @Test
                  public void testExpectedException()
                  {
                    MockedObject mockObj = mock(MockedObj.class);
                    MySubject subject = new MySubject(mockedObj);
                    try {
                      subject.someMethodThrowingException();
                      fail("Expected MyException.");
                    } catch (MyException expected) {
                      assertEquals("My exception message.", expected.getMessage());
                    }
                    verify(mockObj).someCleanup(eq(...));
                  }
                  

                  更新:使用 Java 8 的 lambda 表達式,您可以將函數式接口調用包裝在 try 塊中 簡潔到有用.我想對這種語法的支持會在許多標準測試庫中找到.

                  Update: With Java 8's lambda expressions, you can wrap a functional interface call in a try block concisely enough to be useful. I imagine support for this syntax will find its way into many standard testing libraries.

                  assertThrows(MyException.class,
                      () -> systemUnderTest.throwingMethod());
                  

                  這篇關于異常Junit 4.10后的Mockito驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數轉換為整數?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)

                  <legend id='VHhyw'><style id='VHhyw'><dir id='VHhyw'><q id='VHhyw'></q></dir></style></legend>

                  <small id='VHhyw'></small><noframes id='VHhyw'>

                    <bdo id='VHhyw'></bdo><ul id='VHhyw'></ul>
                  • <tfoot id='VHhyw'></tfoot>
                          <tbody id='VHhyw'></tbody>

                          1. <i id='VHhyw'><tr id='VHhyw'><dt id='VHhyw'><q id='VHhyw'><span id='VHhyw'><b id='VHhyw'><form id='VHhyw'><ins id='VHhyw'></ins><ul id='VHhyw'></ul><sub id='VHhyw'></sub></form><legend id='VHhyw'></legend><bdo id='VHhyw'><pre id='VHhyw'><center id='VHhyw'></center></pre></bdo></b><th id='VHhyw'></th></span></q></dt></tr></i><div class="xtbzrz5" id='VHhyw'><tfoot id='VHhyw'></tfoot><dl id='VHhyw'><fieldset id='VHhyw'></fieldset></dl></div>
                            主站蜘蛛池模板: 土壤有机碳消解器-石油|表层油类分析采水器-青岛溯源环保设备有限公司 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | 雾度仪_雾度计_透光率雾度仪价格-三恩时(3nh)光电雾度仪厂家 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 2-羟基泽兰内酯-乙酰蒲公英萜醇-甘草查尔酮A-上海纯优生物科技有限公司 | 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | 网站优化公司_SEO优化_北京关键词百度快速排名-智恒博网络 | 浙江宝泉阀门有限公司| 科普仪器菏泽市教育教学仪器总厂 | ASA膜,ASA共挤料,篷布色母料-青岛未来化学有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 书法培训-高考书法艺考培训班-山东艺霖书法培训凭实力挺进央美 | 全自动实验室洗瓶机,移液管|培养皿|进样瓶清洗机,清洗剂-广州摩特伟希尔机械设备有限责任公司 | 定量包装秤,吨袋包装称,伸缩溜管,全自动包装秤,码垛机器人,无锡市邦尧机械工程有限公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 阜阳成人高考_阜阳成考报名时间_安徽省成人高考网 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 首页_欧瑞传动官方网站--主营变频器、伺服系统、新能源、软起动器、PLC、HMI | 电梯乘运质量测试仪_电梯安全评估测试仪-武汉懿之刻 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 北京公积金代办/租房发票/租房备案-北京金鼎源公积金提取服务中心 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 湖南成人高考报名-湖南成考网| 强效碱性清洗剂-实验室中性清洗剂-食品级高纯氮气发生器-上海润榕科学器材有限公司 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 断桥铝破碎机_铝合金破碎机_废铁金属破碎机-河南鑫世昌机械制造有限公司 | 水厂自动化|污水处理中控系统|水利信息化|智慧水务|智慧农业-山东德艾自动化科技有限公司 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 办公室家具_板式办公家具定制厂家-FMARTS福玛仕办公家具 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 康明斯发电机,上柴柴油发电机,玉柴柴油发电机组_海南重康电力官网 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 |