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

<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="yiuqmw0" 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() 測(cè)試類

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

      <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="um4ksuw" id='pj9b0'><tfoot id='pj9b0'></tfoot><dl id='pj9b0'><fieldset id='pj9b0'></fieldset></dl></div>

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

                問題描述

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

                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 來測(cè)試這個(gè)類,因?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?

                推薦答案

                對(duì)于未來我會(huì)推薦 Eran Harel 的回答(重構(gòu)將 new 移動(dòng)到可以模擬的工廠).但是,如果您不想更改原始源代碼,請(qǐng)使用非常方便且獨(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í)對(duì)象的間諜.當(dāng)您使用 spy 時(shí),會(huì)調(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)該小心偶爾使用真正的間諜,例如在處理遺留代碼時(shí).

                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() 測(cè)試類的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(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 語句之前的局部變量,這有關(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?)
                <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="wsmq2ww" 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>
                        • 主站蜘蛛池模板: 大型果蔬切片机-水果冬瓜削皮机-洗菜机切菜机-肇庆市凤翔餐饮设备有限公司 | 全温度恒温培养摇床-大容量-立式-远红外二氧化碳培养箱|南荣百科 | 行星齿轮减速机,减速机厂家,山东减速机-淄博兴江机械制造 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易 | 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 打造全球沸石生态圈 - 国投盛世| 施工围挡-施工PVC围挡-工程围挡-深圳市旭东钢构技术开发有限公司 | 早报网 | 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 快速卷帘门_硬质快速卷帘门-西朗门业 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 硅PU球场、篮球场地面施工「水性、环保、弹性」硅PU材料生产厂家-广东中星体育公司 | 耐高温电缆厂家-远洋高温电缆 | 直流大电流电源,燃料电池检漏设备-上海政飞 | 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 环比机械| 硅胶制品-硅橡胶制品-东莞硅胶制品厂家-广东帝博科技有限公司 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 岩石钻裂机-液压凿岩机-劈裂机-挖改钻_湖南烈岩科技有限公司 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 法兰连接型电磁流量计-蒸汽孔板节流装置流量计-北京凯安达仪器仪表有限公司 | 无锡网站建设_小程序制作_网站设计公司_无锡网络公司_网站制作 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 呼末二氧化碳|ETCO2模块采样管_气体干燥管_气体过滤器-湖南纳雄医疗器械有限公司 | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 电加热导热油炉-空气加热器-导热油加热器-翅片电加热管-科安达机械 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 手术示教系统-数字化手术室系统-林之硕医疗云智能视频平台 | 亳州网络公司 - 亳州网站制作 - 亳州网站建设 - 亳州易天科技 | 消防泵-XBD单级卧式/立式消防泵-上海塑泉泵阀(集团)有限公司 |