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

  • <small id='CocuA'></small><noframes id='CocuA'>

      1. <legend id='CocuA'><style id='CocuA'><dir id='CocuA'><q id='CocuA'></q></dir></style></legend>
        <tfoot id='CocuA'></tfoot>
      2. <i id='CocuA'><tr id='CocuA'><dt id='CocuA'><q id='CocuA'><span id='CocuA'><b id='CocuA'><form id='CocuA'><ins id='CocuA'></ins><ul id='CocuA'></ul><sub id='CocuA'></sub></form><legend id='CocuA'></legend><bdo id='CocuA'><pre id='CocuA'><center id='CocuA'></center></pre></bdo></b><th id='CocuA'></th></span></q></dt></tr></i><div class="xxfhpdt" id='CocuA'><tfoot id='CocuA'></tfoot><dl id='CocuA'><fieldset id='CocuA'></fieldset></dl></div>
          <bdo id='CocuA'></bdo><ul id='CocuA'></ul>
      3. 在 Java 中模擬文件 - 模擬內(nèi)容 - Mockito

        Mocking Files in Java - Mock Contents - Mockito(在 Java 中模擬文件 - 模擬內(nèi)容 - Mockito)
        • <bdo id='74Ugc'></bdo><ul id='74Ugc'></ul>

        • <tfoot id='74Ugc'></tfoot>

                <tbody id='74Ugc'></tbody>

              1. <legend id='74Ugc'><style id='74Ugc'><dir id='74Ugc'><q id='74Ugc'></q></dir></style></legend>
              2. <small id='74Ugc'></small><noframes id='74Ugc'>

                  <i id='74Ugc'><tr id='74Ugc'><dt id='74Ugc'><q id='74Ugc'><span id='74Ugc'><b id='74Ugc'><form id='74Ugc'><ins id='74Ugc'></ins><ul id='74Ugc'></ul><sub id='74Ugc'></sub></form><legend id='74Ugc'></legend><bdo id='74Ugc'><pre id='74Ugc'><center id='74Ugc'></center></pre></bdo></b><th id='74Ugc'></th></span></q></dt></tr></i><div class="pnbdt5v" id='74Ugc'><tfoot id='74Ugc'></tfoot><dl id='74Ugc'><fieldset id='74Ugc'></fieldset></dl></div>

                1. 本文介紹了在 Java 中模擬文件 - 模擬內(nèi)容 - Mockito的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我對(duì)模擬還很陌生,我一直在嘗試模擬實(shí)際內(nèi)容(基本上只在內(nèi)存中創(chuàng)建一個(gè)虛擬文件),以便在任何時(shí)候都不會(huì)將數(shù)據(jù)寫(xiě)入磁盤(pán).

                  I'm pretty new to mocking, and I've been trying to mock the actual contents (essentially create a virtual file in memory alone) so that no data is written to disk at any point.

                  我嘗試了一些解決方案,例如模擬文件并模擬盡可能多的屬性,然后使用文件寫(xiě)入器/緩沖寫(xiě)入器寫(xiě)入文件,但這些都不能很好地工作,因?yàn)樗麄冃枰?guī)范路徑.任何人都找到了除此或類似之外的解決方案,但我正在接近這個(gè)錯(cuò)誤?

                  I've tried solutions like mocking the file and mocking as many of the properties that I can figure out as much as possible, an then also writing into it with a filewriter/bufferedwriter, but those don't work well, since they need canonical paths. Anyone found a solution other than this or similar, but that I'm approaching this wrong?

                  我一直是這樣的:

                  private void mocking(){
                      File badHTML = mock(File.class);
                      //setting the properties of badHTML
                      when(badHTML.canExecute()).thenReturn(Boolean.FALSE);
                      when(badHTML.canRead()).thenReturn(Boolean.TRUE);
                      when(badHTML.canWrite()).thenReturn(Boolean.TRUE);
                      when(badHTML.compareTo(badHTML)).thenReturn(Integer.SIZE);
                      when(badHTML.delete()).thenReturn(Boolean.FALSE);
                      when(badHTML.getFreeSpace()).thenReturn(0l);
                      when(badHTML.getName()).thenReturn("bad.html");
                      when(badHTML.getParent()).thenReturn(null);
                      when(badHTML.getPath()).thenReturn("bad.html");
                      when(badHTML.getParentFile()).thenReturn(null);
                      when(badHTML.getTotalSpace()).thenReturn(0l);
                      when(badHTML.isAbsolute()).thenReturn(Boolean.FALSE);
                      when(badHTML.isDirectory()).thenReturn(Boolean.FALSE);
                      when(badHTML.isFile()).thenReturn(Boolean.TRUE);
                      when(badHTML.isHidden()).thenReturn(Boolean.FALSE);
                      when(badHTML.lastModified()).thenReturn(System.currentTimeMillis());
                      when(badHTML.mkdir()).thenReturn(Boolean.FALSE);
                      when(badHTML.mkdirs()).thenReturn(Boolean.FALSE);
                      when(badHTML.setReadOnly()).thenReturn(Boolean.FALSE);
                      when(badHTML.setExecutable(true)).thenReturn(Boolean.FALSE);
                      when(badHTML.setExecutable(false)).thenReturn(Boolean.TRUE);
                      when(badHTML.setReadOnly()).thenReturn(Boolean.FALSE);
                  
                      try {
                          BufferedWriter bw = new BufferedWriter(new FileWriter(badHTML));
                          /*
                            badHTMLText is a string with the contents i want to put into the file, 
                            can be just about whatever you want
                           */
                          bw.append(badHTMLText);
                          bw.close();
                  
                      } catch (IOException ex) {
                          System.err.println(ex);
                      }
                  }
                  

                  任何想法或指導(dǎo)都會(huì)非常有幫助.在此之后的某個(gè)地方,我基本上嘗試使用另一個(gè)類從文件中讀取.我會(huì)嘗試模擬某種輸入流,但另一個(gè)類不接受輸入流,因?yàn)樗琼?xiàng)目的 io 處理類.

                  Any ideas or guidance would be very helpful. Somewhere after this i basically try to read from the file using another class. I would try to mock some sort of input stream, but the other class doesn't take an inputstream, since it's the io handling class for the project.

                  推薦答案

                  你似乎在追求矛盾的目標(biāo).一方面,您試圖避免將數(shù)據(jù)寫(xiě)入磁盤(pán),這在測(cè)試中并不是一個(gè)壞目標(biāo).另一方面,您正在嘗試測(cè)試您的 I/O 處理類,這意味著您將使用假定您的 File 將與本機(jī)調(diào)用一起使用的系統(tǒng)實(shí)用程序.因此,這是我的指導(dǎo):

                  You seem to be after contradictory goals. On the one hand, you're trying to avoid writing data to disk, which isn't a bad goal in tests. On the other, you're trying to test your I/O-handling class, which means you'll be working with system utilities that assume that your File will work with native calls. As such, here's my guidance:

                  • 不要試圖模擬 File.只是不要.太多原生事物依賴它.
                  • 如果可以,請(qǐng)將您的 I/O 處理代碼分成打開(kāi) File 并將其轉(zhuǎn)換為 Reader 的一半,以及解析 HTML 的一半閱讀器.
                  • 此時(shí),您根本不需要模擬 - 只需構(gòu)造一個(gè) StringReader 來(lái)模擬數(shù)據(jù)源.
                  • 雖然它可以很好地處理您的單元測(cè)試,但您可能還想編寫(xiě)一個(gè)使用 集成測(cè)試temp-file-in-java">臨時(shí)文件 并確保它讀取正確.(感謝 Brice 添加提示!)
                  • Don't try to mock a File. Just don't. Too many native things depend on it.
                  • If you can, split your I/O-handling code into the half that opens a File and turns it into a Reader, and the half that parses HTML out of the Reader.
                  • At that point, you don't need a mock at all--just construct a StringReader to simulate the data source.
                  • While that handles your unit tests pretty well, you may also want to write an integration test that uses a temporary file and ensure that it reads right. (Thanks Brice for adding that tip!)

                  不要害怕重構(gòu)你的類以使測(cè)試更容易,如下所示:

                  Don't be afraid to refactor your class to make testing easier, as here:

                  class YourClass {
                    public int method(File file) {
                      // do everything here, which is why it requires a mock
                    }   
                  }   
                  
                  class YourRefactoredClass {
                    public int method(File file) {
                      return methodForTest(file.getName(), file.isFile(),
                          file.isAbsolute(), new FileReader(file));
                    }   
                  
                    /** For testing only. */
                    int methodForTest(
                        String name, boolean isFile, boolean isAbsolute, Reader fileContents) {
                      // actually do the calculation here
                    }   
                  }   
                  
                  class YourTest {
                    @Test public int methodShouldParseBadHtml() {
                      YourRefactoredClass yrc = new YourRefactoredClass();
                      assertEquals(42, yrc.methodForTest(
                          "bad.html", true, false, new StringReader(badHTMLText));
                    }   
                  }   
                  

                  此時(shí) method 中的邏輯非常簡(jiǎn)單,不值得測(cè)試,methodForTest 中的邏輯非常容易訪問(wèn),您可以對(duì)其進(jìn)行大量測(cè)試.

                  At this point the logic in method is so straightforward it's not worth testing, and the logic in methodForTest is so easy to access that you can test it heavily.

                  這篇關(guān)于在 Java 中模擬文件 - 模擬內(nèi)容 - Mockito的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測(cè) 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語(yǔ)句之前的局部變量,這有關(guān)系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲(chǔ)為 int?)
                2. <tfoot id='eLhaX'></tfoot>

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

                            <tbody id='eLhaX'></tbody>

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

                          • <bdo id='eLhaX'></bdo><ul id='eLhaX'></ul>
                            <legend id='eLhaX'><style id='eLhaX'><dir id='eLhaX'><q id='eLhaX'></q></dir></style></legend>
                            主站蜘蛛池模板: 新车测评网_网罗汽车评测资讯_汽车评测门户报道 | 青岛代理记账_青岛李沧代理记账公司_青岛崂山代理记账一个月多少钱_青岛德辉财税事务所官网 | 定量包装秤,吨袋包装称,伸缩溜管,全自动包装秤,码垛机器人,无锡市邦尧机械工程有限公司 | 中央空调维修、中央空调保养、螺杆压缩机维修-苏州东菱空调 | 精密五金加工厂-CNC数控车床加工_冲压件|蜗杆|螺杆加工「新锦泰」 | 谈股票-今日股票行情走势分析-牛股推荐排行榜 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 箱式破碎机_移动方箱式破碎机/价格/厂家_【华盛铭重工】 | 骨密度检测仪_骨密度分析仪_骨密度仪_动脉硬化检测仪专业生产厂家【品源医疗】 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 常州律师事务所_常州律所_常州律师-江苏乐天律师事务所 | 物流之家新闻网-最新物流新闻|物流资讯|物流政策|物流网-匡匡奈斯物流科技 | 精密五金加工厂-CNC数控车床加工_冲压件|蜗杆|螺杆加工「新锦泰」 | EPK超声波测厚仪,德国EPK测厚仪维修-上海树信仪器仪表有限公司 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 济南轻型钢结构/济南铁艺护栏/济南铁艺大门-济南燕翔铁艺制品有限公司 | 消防设施操作员考试报名时间,报名入口,报考条件 | 液压压力机,液压折弯机,液压剪板机,模锻液压机-鲁南新力机床有限公司 | 全自动包衣机-无菌分装隔离器-浙江迦南科技股份有限公司 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | 合肥注册公司|合肥代办营业执照、2024注册公司流程 | 铝合金脚手架厂家-专注高空作业平台-深圳腾达安全科技 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 北钻固控设备|石油钻采设备-石油固控设备厂家 | 塑料托盘厂家直销-吹塑托盘生产厂家-力库塑业【官网】 | 新中天检测有限公司青岛分公司-山东|菏泽|济南|潍坊|泰安防雷检测验收 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | 球形钽粉_球形钨粉_纳米粉末_难熔金属粉末-广东银纳官网 | 南京泽朗生物科技有限公司 | VOC检测仪-甲醛检测仪-气体报警器-气体检测仪厂家-深恒安科技有限公司 | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 电子厂招聘_工厂招聘_普工招聘_小时工招聘信息平台-众立方招工网 | PCB设计,PCB抄板,电路板打样,PCBA加工-深圳市宏力捷电子有限公司 | LCD3D打印机|教育|桌面|光固化|FDM3D打印机|3D打印设备-广州造维科技有限公司 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 双齿辊破碎机-大型狼牙破碎机视频-对辊破碎机价格/型号图片-金联机械设备生产厂家 |