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

    <legend id='PgzHN'><style id='PgzHN'><dir id='PgzHN'><q id='PgzHN'></q></dir></style></legend>

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

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

    1. Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?

      What#39;s the difference between Mockito Matchers isA, any, eq, and same?(Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?)

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

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

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

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

              • 本文介紹了Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我對它們之間的區(qū)別以及在哪種情況下選擇哪個感到困惑.有些區(qū)別可能很明顯,例如 anyeq,但我將它們都包括在內(nèi)只是為了確定.

                I am confused on what's the difference between them, and which one to choose in which case. Some difference might be obvious, like any and eq, but I'm including them all just to be sure.

                我想知道它們的區(qū)別,因為我遇到了這個問題:我在 Controller 類中有這個 POST 方法

                I wonder about their differences because I came across this problem: I have this POST method in a Controller class

                public Response doSomething(@ResponseBody Request request) {
                    return someService.doSomething(request);
                }
                

                并且想對該控制器執(zhí)行單元測試.我有兩個版本.第一個是簡單的,像這樣

                And would like to perform a unit test on that controller. I have two versions. The first one is the simple one, like this

                @Test
                public void testDoSomething() {
                    //initialize ObjectMapper mapper
                    //initialize Request req and Response res
                    
                    when(someServiceMock.doSomething(req)).thenReturn(res);
                
                    Response actualRes = someController.doSomething(req);
                    assertThat(actualRes, is(res));
                }
                

                但我想使用 MockMvc 方法,比如這個

                But I wanted to use a MockMvc approach, like this one

                @Test
                public void testDoSomething() {
                    //initialize ObjectMapper mapper
                    //initialize Request req and Response res
                    
                    when(someServiceMock.doSomething(any(Request.class))).thenReturn(res);
                
                    mockMvc.perform(post("/do/something")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content(mapper.writeValueAsString(req))
                    )
                            .andExpect(status().isOk())
                            .andExpect(jsonPath("$message", is("done")));
                }
                

                兩者都運作良好.但我希望我的 someServiceMock.doSomething() 在 MockMvc 方法中接收 req,或者至少是一個與 req(不僅僅是任何 Request 類),并返回 res,就像第一個一樣.我知道使用 MockMvc 方法是不可能的(或者是嗎?),因為在實際調(diào)用中傳遞的對象總是與在模擬中傳遞的對象不同.無論如何我可以做到這一點嗎?或者這樣做是否有意義?或者我應(yīng)該對使用 any(Request.class) 感到滿意嗎?我試過eqsame,但都失敗了.

                Both work well. But I wanted my someServiceMock.doSomething() in the MockMvc approach to receive req, or at least an object that has the same variable values as req (not just any Request class), and return res, just like the first. I know that it's impossible using the MockMvc approach (or is it?), because the object passed in the actual call is always different from the object passed in the mock. Is there anyway I can achieve that? Or does it even make sense to do that? Or should I be satisfied using any(Request.class)? I've tried eq, same, but all of them fail.

                推薦答案

                • any() 絕對不檢查任何內(nèi)容.從 Mockito 2.0 開始,any(T.class) 共享 isA 語義以表示任何 T";或正確地T 類型的任何實例".

                  • any() checks absolutely nothing. Since Mockito 2.0, any(T.class) shares isA semantics to mean "any T" or properly "any instance of type T".

                    這是與 Mockito 1.x 相比的一個變化,其中 any(T.class) 完全沒有檢查但在 Java 8 之前保存了一個強(qiáng)制轉(zhuǎn)換:任何類型的對象,對于給定的類不是必需的.提供類參數(shù)只是為了避免強(qiáng)制轉(zhuǎn)換."

                    This is a change compared to Mockito 1.x, where any(T.class) checked absolutely nothing but saved a cast prior to Java 8: "Any kind object, not necessary of the given class. The class argument is provided only to avoid casting."

                    isA(T.class) 檢查參數(shù) instanceof T 是否為非空.

                    same(obj) 檢查參數(shù)是否引用與 obj 相同的實例,使得 arg == obj 為真.

                    same(obj) checks that the argument refers to the same instance as obj, such that arg == obj is true.

                    eq(obj) 根據(jù)其 equals 方法檢查參數(shù)是否等于 obj.如果您在不使用匹配器的情況下傳遞實際值,這也是這種行為.

                    eq(obj) checks that the argument equals obj according to its equals method. This is also the behavior if you pass in real values without using matchers.

                    請注意,除非 equals 被覆蓋,否則您將看到默認(rèn)的 Object.equals 實現(xiàn),其行為與 same(obj) 相同.

                    Note that unless equals is overridden, you'll see the default Object.equals implementation, which would have the same behavior as same(obj).

                    如果您需要更精確的自定義,您可以為自己的謂詞使用適配器:

                    If you need more exact customization, you can use an adapter for your own predicate:

                    • 對于 Mockito 1.x,請使用 argThat 帶有自定義 Hamcrest Matcher,可以準(zhǔn)確選擇您需要的對象.
                    • 對于 Mockito 2.0 及更高版本,請使用 Matchers.argThat 與自定義 org.mockito.ArgumentMatcherMockitoHamcrest.argThat使用自定義 Hamcrest Matcher<T>.
                    • For Mockito 1.x, use argThat with a custom Hamcrest Matcher<T> that selects exactly the objects you need.
                    • For Mockito 2.0 and beyond, use Matchers.argThat with a custom org.mockito.ArgumentMatcher<T>, or MockitoHamcrest.argThat with a custom Hamcrest Matcher<T>.

                    您也可以使用 refEq,它使用reflection來確認(rèn)對象相等;Hamcrest 與公共 bean 的 SamePropertyValuesAs 有類似的實現(xiàn)-樣式屬性.請注意,在 GitHub issue #1800 上建議棄用和刪除 refEq,并且在那個問題中,您可能更喜歡 eq 來更好地為您的類提供更好的封裝,而不是它們的平等感.

                    You may also use refEq, which uses reflection to confirm object equality; Hamcrest has a similar implementation with SamePropertyValuesAs for public bean-style properties. Note that on GitHub issue #1800 proposes deprecating and removing refEq, and as in that issue you might prefer eq to better give your classes better encapsulation over their sense of equality.

                    這篇關(guān)于Mockito Matchers isA、any、eq 和 same 有什么區(qū)別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='YXsmr'></tfoot>
                      <tbody id='YXsmr'></tbody>
                      <bdo id='YXsmr'></bdo><ul id='YXsmr'></ul>
                      <legend id='YXsmr'><style id='YXsmr'><dir id='YXsmr'><q id='YXsmr'></q></dir></style></legend>

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

                        • <i id='YXsmr'><tr id='YXsmr'><dt id='YXsmr'><q id='YXsmr'><span id='YXsmr'><b id='YXsmr'><form id='YXsmr'><ins id='YXsmr'></ins><ul id='YXsmr'></ul><sub id='YXsmr'></sub></form><legend id='YXsmr'></legend><bdo id='YXsmr'><pre id='YXsmr'><center id='YXsmr'></center></pre></bdo></b><th id='YXsmr'></th></span></q></dt></tr></i><div class="k2i2qae" id='YXsmr'><tfoot id='YXsmr'></tfoot><dl id='YXsmr'><fieldset id='YXsmr'></fieldset></dl></div>
                          主站蜘蛛池模板: 深圳市东信高科自动化设备有限公司 | 无刷电机_直流无刷电机_行星减速机-佛山市藤尺机电设备有限公司 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | PAS糖原染色-CBA流式多因子-明胶酶谱MMP-上海研谨生物科技有限公司 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 钢板仓,大型钢板仓,钢板库,大型钢板库,粉煤灰钢板仓,螺旋钢板仓,螺旋卷板仓,骨料钢板仓 | 股票入门基础知识_股票知识_股票投资大师_格雷厄姆网 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 开平机_纵剪机厂家_开平机生产厂家|诚信互赢-泰安瑞烨精工机械制造有限公司 | 不锈钢发酵罐_水果酒发酵罐_谷物发酵罐_山东誉诚不锈钢制品有限公司 | 全自动面膜机_面膜折叠机价格_面膜灌装机定制_高速折棉机厂家-深圳市益豪科技有限公司 | 骨龄仪_骨龄检测仪_儿童骨龄测试仪_品牌生产厂家【品源医疗】 | 高防护蠕动泵-多通道灌装系统-高防护蠕动泵-www.bjhuiyufluid.com慧宇伟业(北京)流体设备有限公司 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 自动化展_机器人展_机床展_工业互联网展_广东佛山工博会 | 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | 聚氨酯复合板保温板厂家_廊坊华宇创新科技有限公司 | 宁波普瑞思邻苯二甲酸盐检测仪,ROHS2.0检测设备,ROHS2.0测试仪厂家 | 成都装修公司-成都装修设计公司推荐-成都朗煜装饰公司 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 领袖户外_深度旅游、摄影旅游、小团慢旅行、驴友网 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 无负压供水设备,消防稳压供水设备-淄博创辉供水设备有限公司 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 玻璃钢板-玻璃钢防腐瓦-玻璃钢材料-广东壹诺 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | [品牌官网]贵州遵义双宁口腔连锁_贵州遵义牙科医院哪家好_种植牙_牙齿矫正_原华美口腔 | 818手游网_提供当下热门APP手游_最新手机游戏下载 | NMRV减速机|铝合金减速机|蜗轮蜗杆减速机|NMRV减速机厂家-东莞市台机减速机有限公司 | X光检测仪_食品金属异物检测机_X射线检测设备_微现检测 | 据信,上课带着跳 D 体验-别样的课堂刺激感受引发网友热议 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 快速门厂家批发_PVC快速卷帘门_高速门_高速卷帘门-广州万盛门业 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 | 红外光谱仪维修_二手红外光谱仪_红外压片机_红外附件-天津博精仪器 | 陶瓷砂磨机,盘式砂磨机,棒销式砂磨机-无锡市少宏粉体科技有限公司 |