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

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

    1. <legend id='59fqG'><style id='59fqG'><dir id='59fqG'><q id='59fqG'></q></dir></style></legend>
    2. <small id='59fqG'></small><noframes id='59fqG'>

        <bdo id='59fqG'></bdo><ul id='59fqG'></ul>

    3. <tfoot id='59fqG'></tfoot>

      使用 Mockito 在其中調(diào)用 new() 測試類

      Test class with a new() call in it with Mockito(使用 Mockito 在其中調(diào)用 new() 測試類)

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

      • <bdo id='pj9b0'></bdo><ul id='pj9b0'></ul>

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

                  <tbody id='pj9b0'></tbody>
                本文介紹了使用 Mockito 在其中調(diào)用 new() 測試類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有一個包含 new() 調(diào)用以實(shí)例化 LoginContext 對象的舊類:

                I have a legacy class that contains a new() call to instantiate a LoginContext object:

                public class TestedClass {
                  public LoginContext login(String user, String password) {
                    LoginContext lc = new LoginContext("login", callbackHandler);
                  }
                }
                

                我想使用 Mockito 來模擬 LoginContext 來測試這個類,因?yàn)樗笤趯?shí)例化之前設(shè)置 JAAS 安全性內(nèi)容,但我不確定如何在不更改 code>login() 方法來外部化 LoginContext.

                I want to test this class using Mockito to mock the LoginContext as it requires that the JAAS security stuff be set up before instantiating, but I'm not sure how to do that without changing the login() method to externalize the LoginContext.

                是否可以使用 Mockito 來模擬 LoginContext 類?

                Is it possible using Mockito to mock the LoginContext class?

                推薦答案

                對于未來我會推薦 Eran Harel 的回答(重構(gòu)將 new 移動到可以模擬的工廠).但是,如果您不想更改原始源代碼,請使用非常方便且獨(dú)特的功能:sies.來自文檔:

                For the future I would recommend Eran Harel's answer (refactoring moving new to factory that can be mocked). But if you don't want to change the original source code, use very handy and unique feature: spies. From the documentation:

                您可以創(chuàng)建真實(shí)對象的間諜.當(dāng)您使用 spy 時,會調(diào)用 real 方法(除非方法被存根).

                You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).

                應(yīng)該小心偶爾使用真正的間諜,例如在處理遺留代碼時.

                Real spies should be used carefully and occasionally, for example when dealing with legacy code.

                在你的情況下,你應(yīng)該寫:

                In your case you should write:

                TestedClass tc = spy(new TestedClass());
                LoginContext lcMock = mock(LoginContext.class);
                when(tc.login(anyString(), anyString())).thenReturn(lcMock);
                

                這篇關(guān)于使用 Mockito 在其中調(diào)用 new() 測試類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='OQVUS'></tfoot>
                <legend id='OQVUS'><style id='OQVUS'><dir id='OQVUS'><q id='OQVUS'></q></dir></style></legend>

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

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

                  <tbody id='OQVUS'></tbody>
                • <bdo id='OQVUS'></bdo><ul id='OQVUS'></ul>
                        • 主站蜘蛛池模板: 91精品国产91久久综合桃花 | 国产精品一区二区欧美 | 四虎影院新地址 | 亚洲成人av一区二区 | 日本免费视频在线观看 | 久久国产一区 | 久久精品国产一区二区三区不卡 | 日本中文字幕视频 | 精品视频在线一区 | 97在线播放| 天天澡天天狠天天天做 | 人人鲁人人莫人人爱精品 | 亚洲黄色一级毛片 | 免费成人毛片 | 一区二区精品电影 | 色眯眯视频在线观看 | 精品久久久久久久久久久 | 操操日| 精品美女在线观看视频在线观看 | 亚洲精品一区二区三区在线 | 亚洲国产精品一区二区三区 | 成人区精品一区二区婷婷 | 午夜噜噜噜 | 岛国毛片 | 福利网址 | 夜夜夜夜夜夜曰天天天 | 日韩欧美在线一区 | 韩日在线| 国产成人精品一区二三区在线观看 | 男人天堂999 | 免费精品久久久久久中文字幕 | 国产精品美女www爽爽爽视频 | 中文av电影| 国产综合久久 | 亚洲精品粉嫩美女一区 | 日韩精品一区二区三区视频播放 | 99reav| 97视频网站 | 国产精品入口 | 九九伦理电影 | 欧美国产日本一区 |