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

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

      2. <tfoot id='icfCt'></tfoot>

        Mockito - thenReturn 總是返回空對象

        Mockito - thenReturn always returns null object(Mockito - thenReturn 總是返回空對象)
              <bdo id='L6W2c'></bdo><ul id='L6W2c'></ul>

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

                    <tbody id='L6W2c'></tbody>

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

                1. 本文介紹了Mockito - thenReturn 總是返回空對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試實現 Mockito 來測試特定方法,但 .thenReturn(...) 似乎總是返回一個空對象,而不是我想要的:

                  I'm trying to implement Mockito to test a particular method but the .thenReturn(...) seems to always be returning a null object instead of what I intended:

                  剪切:

                  public class TestClassFacade {
                  
                    // injected via Spring
                    private InterfaceBP bpService;
                  
                    public void setBpService(InterfaceBP bpService) {
                  
                        this.bpService = bpService;
                    }
                  
                    public TestVO getTestData(String testString) throws Exception {
                  
                      BPRequestVO bpRequestVO = new BPRequestVO();
                  
                      bpRequestVO.setGroupNumber(testString) ;
                      bpRequestVO.setProductType("ALL") ;           
                      bpRequestVO.setProfileType("Required - TEST") ;
                  
                      IBPServiceResponse serviceResponse = bpService.getProduct(bpRequestVO);  //PROBLEM
                  
                      if (serviceResponse.getMessage().equalsIgnoreCase("BOB")) {
                  
                          throw new Exception();
                  
                      } else {
                  
                          TestVO testVO = new TestVO();
                      }
                  
                      return testVO;
                    }
                  
                  }
                  

                  彈簧配置:

                  <bean id="testClass" class="com.foo.TestClassFacade">
                  
                     <property name="bpService" ref="bpService" />
                  
                  </bean>
                  
                  <bean id="bpService" class="class.cloud.BPService" />
                  

                  Mockito 測試方法:

                  @RunWith(MockitoJUnitRunner.class)
                  public class BaseTest {
                  
                      @Mock BPService mockBPService;
                      @InjectMocks TestClassFacade mockTestClassFacade;
                  
                      private String testString = null;
                      private BPRequestVO someBPRequestVO = new BPRequestVO();
                      private IBPServiceResponse invalidServiceResponse = new BPServiceResponse();
                  
                      @Test (expected = Exception.class)
                      public void getBPData_bobStatusCode_shouldThrowException() throws Exception {
                  
                          invalidServiceResponse.setMessage("BOB");
                  
                          someBPRequestVO.setGroupNumber(null);
                          someBPRequestVO.setProductType("ALL");
                          someBPRequestVO.setProfileType("Required - TEST");
                  
                          System.out.println("1: " + someBPRequestVO.getGroupNumber());
                          System.out.println("2: " + someBPRequestVO.getProductType());
                          System.out.println("3: " + someBPRequestVO.getProfileType());
                          System.out.println("4: " + someBPRequestVO.getEffectiveDate());
                  
                          when(mockBPService.getProduct(someBPRequestVO)).thenReturn(invalidServiceResponse);
                  
                          mockTestClassFacade.getTestData(testString);
                  
                          verify(mockBPService).getProduct(someBPRequestVO);
                      }
                  }
                  

                  系統輸出:

                  1: null
                  2: ALL
                  3: Required - TEST
                  4: null
                  

                  這里發生的情況是,當我運行測試時,serviceResponse 對象在上面標有//PROBLEM 的 CUT 行上為 null.我的愿望是用我的測試方法中的invalidServiceResponse"對象填充該對象.從我的 System.out.println 的輸出來看,我的 bpRequestVO 似乎與我的 someBPRequestVO 在內容上相匹配.

                  What's happening here is that when I run the test the serviceResponse object is null on the line in the CUT marked with //PROBLEM above. My desire is to have that object be populated with my "invalidServiceResponse" object from my test method. Judging from the output of my System.out.println's it appears that my bpRequestVO matches my someBPRequestVO in content.

                  誰能告訴我我在這里缺少什么?

                  Could some one show me what I'm missing here?

                  感謝您的寶貴時間!

                  推薦答案

                  when() 中使用的 BPRequestVO 實例與 getTestData() 中使用的實例不同代碼>.
                  除非您覆蓋 equals(),否則它們將不匹配.

                  The instance of BPRequestVO that you use with when() is different than the one used in getTestData().
                  Unless you override equals(), they will not match.

                  如果你重寫 equals(),你應該不需要編寫自定義 Matcher.請注意 Mockito 文檔中的以下內容:

                  You should not need to write a custom Matcher if you override equals(). Note the following from the Mockito documentation:

                  自定義參數匹配器會使測試的可讀性降低.有時最好為傳遞給 mock 的參數實現 equals()(Mockito 自然使用 equals() 進行參數匹配).這可以使測試更清晰."

                  "Custom argument matchers can make the test less readable. Sometimes it's better to implement equals() for arguments that are passed to mocks (Mockito naturally uses equals() for argument matching). This can make the test cleaner."

                  這篇關于Mockito - thenReturn 總是返回空對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)
                    • <small id='wWQFP'></small><noframes id='wWQFP'>

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

                        <legend id='wWQFP'><style id='wWQFP'><dir id='wWQFP'><q id='wWQFP'></q></dir></style></legend>
                          <bdo id='wWQFP'></bdo><ul id='wWQFP'></ul>
                            主站蜘蛛池模板: 99视频在线 | 亚洲欧美一区二区三区在线 | 黄色片网站在线观看 | 嫩草视频在线免费观看 | 成人高清视频在线观看 | aaaa一级毛片 | 亚洲天堂av在线 | 久久久久久久久蜜桃 | 精品福利一区二区三区 | 成人亚洲精品 | 宅女噜噜66国产精品观看免费 | 欧美午夜一区 | 99精品国产一区二区三区 | 精品真实国产乱文在线 | 亚洲成人精品 | 国产在线视频一区二区 | 国产日韩一区二区三区 | 日韩精品一区二区三区在线播放 | 狠狠夜夜 | 免费亚洲视频 | 国产精品成人在线观看 | 成人激情视频免费观看 | 99精品国自产在线 | 亚洲精品一区二区三区免 | 一区二区不卡视频 | 特黄特色大片免费视频观看 | 欧美日韩综合一区 | 久久精品久久综合 | 日本一区二区不卡 | 国产精品免费小视频 | 91免费在线 | 中文字幕av在线播放 | 黄色大片在线 | 成人黄色电影在线播放 | 99pao成人国产永久免费视频 | 日韩一级欧美一级 | 日韩中文字幕一区 | 玖玖精品 | 日日夜夜精品视频 | 国产精品久久久99 | 伊伊综合网 |