久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

        <bdo id='nK1M2'></bdo><ul id='nK1M2'></ul>
      <tfoot id='nK1M2'></tfoot>

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

      1. <small id='nK1M2'></small><noframes id='nK1M2'>

        如何使用 mockito 模擬字符串?

        How to mock a String using mockito?(如何使用 mockito 模擬字符串?)
        1. <legend id='yD40b'><style id='yD40b'><dir id='yD40b'><q id='yD40b'></q></dir></style></legend>
          • <i id='yD40b'><tr id='yD40b'><dt id='yD40b'><q id='yD40b'><span id='yD40b'><b id='yD40b'><form id='yD40b'><ins id='yD40b'></ins><ul id='yD40b'></ul><sub id='yD40b'></sub></form><legend id='yD40b'></legend><bdo id='yD40b'><pre id='yD40b'><center id='yD40b'></center></pre></bdo></b><th id='yD40b'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='yD40b'><tfoot id='yD40b'></tfoot><dl id='yD40b'><fieldset id='yD40b'></fieldset></dl></div>

                  <tbody id='yD40b'></tbody>
                  <bdo id='yD40b'></bdo><ul id='yD40b'></ul>
                  <tfoot id='yD40b'></tfoot>

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

                  本文介紹了如何使用 mockito 模擬字符串?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我需要模擬一個測試場景,在該場景中我調(diào)用 String 對象的 getBytes() 方法并得到 UnsupportedEncodingException.

                  I need to simulate a test scenario in which I call the getBytes() method of a String object and I get an UnsupportedEncodingException.

                  我已嘗試使用以下代碼來實(shí)現(xiàn):

                  I have tried to achieve that using the following code:

                  String nonEncodedString = mock(String.class);
                  when(nonEncodedString.getBytes(anyString())).thenThrow(new UnsupportedEncodingException("Parsing error."));
                  

                  問題是,當(dāng)我運(yùn)行我的測試用例時,我得到一個 MockitoException,它說我無法模擬 java.lang.String 類.

                  The problem is that when I run my test case I get a MockitoException that says that I can't mock a java.lang.String class.

                  有沒有辦法使用 mockito 來模擬 String 對象,或者,當(dāng)我調(diào)用 getBytes 方法時,是否可以讓我的 String 對象拋出 UnsupportedEncodingException?

                  Is there a way to mock a String object using mockito or, alternatively, a way to make my String object throw an UnsupportedEncodingException when I call the getBytes method?

                  這里有更多細(xì)節(jié)來說明問題:

                  Here are more details to illustrate the problem:

                  這是我要測試的類:

                  public final class A {
                      public static String f(String str){
                          try {
                              return new String(str.getBytes("UTF-8"));
                          } catch (UnsupportedEncodingException e) {
                              // This is the catch block that I want to exercise.
                              ...
                          }
                      }
                  }
                  

                  這是我的測試類(我正在使用 JUnit 4 和 mockito):

                  This is my testing class (I'm using JUnit 4 and mockito):

                  public class TestA {
                  
                      @Test(expected=UnsupportedEncodingException.class)
                      public void test(){
                          String aString = mock(String.class);
                          when(nonEncodedString.getBytes(anyString())).thenThrow(new UnsupportedEncodingException("Parsing error."));
                          A.f(aString);
                      }
                  }
                  

                  推薦答案

                  問題是Java中的String類被標(biāo)記為final,所以使用傳統(tǒng)的mocking框架不能mock.根據(jù) Mockito FAQ,這也是該框架的限制.

                  The problem is the String class in Java is marked as final, so you cannot mock is using traditional mocking frameworks. According to the Mockito FAQ, this is a limitation of that framework as well.

                  這篇關(guān)于如何使用 mockito 模擬字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(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)建一個隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)
                • <tfoot id='B6yDK'></tfoot>

                      <legend id='B6yDK'><style id='B6yDK'><dir id='B6yDK'><q id='B6yDK'></q></dir></style></legend>
                        <tbody id='B6yDK'></tbody>

                            <bdo id='B6yDK'></bdo><ul id='B6yDK'></ul>

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

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

                            主站蜘蛛池模板: 久久久久久久久久久高潮一区二区 | 精品一区在线 | 亚洲精品乱码久久久久久蜜桃 | 欧洲精品在线观看 | www.毛片 | 色婷婷精品久久二区二区蜜臂av | 亚洲精品久久久久久久久久久 | 久久精品欧美一区二区三区不卡 | 丝袜美腿一区二区三区动态图 | 成人a在线观看 | 国产片侵犯亲女视频播放 | 国产精品毛片久久久久久 | 欧美精品一区二区在线观看 | 亚洲午夜久久久 | 91精品国产麻豆 | 黄色毛片免费看 | 免费观看一级毛片 | 欧美日高清视频 | 精国产品一区二区三区四季综 | 国产精品1区2区 | 亚洲日产精品 | 超碰在线97国产 | 精品久久久久久亚洲国产800 | 91看片在线观看 | av免费网站在线观看 | 精品国产一区二区三区性色av | 亚洲男人网| 久久久123| 国产精品污污视频 | 黄色一级视频免费 | 在线观看不卡av | 亚洲视频免费观看 | 美国av毛片 | 亚洲 欧美 另类 综合 偷拍 | 久久精品国产清自在天天线 | 色婷综合网 | 欧美一级二级在线观看 | 亚洲欧美一区二区三区国产精品 | 免费国产精品久久久久久 | 九九精品在线 | 欧美国产91 |