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

<tfoot id='MAaXL'></tfoot>

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

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

      <bdo id='MAaXL'></bdo><ul id='MAaXL'></ul>
    1. <legend id='MAaXL'><style id='MAaXL'><dir id='MAaXL'><q id='MAaXL'></q></dir></style></legend>
    2. 基于參數屬性的模擬返回值

      mockito return value based on property of a parameter(基于參數屬性的模擬返回值)
            <tbody id='AoLYd'></tbody>

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

              <legend id='AoLYd'><style id='AoLYd'><dir id='AoLYd'><q id='AoLYd'></q></dir></style></legend>
              • <bdo id='AoLYd'></bdo><ul id='AoLYd'></ul>
                <tfoot id='AoLYd'></tfoot>

              • <small id='AoLYd'></small><noframes id='AoLYd'>

                本文介紹了基于參數屬性的模擬返回值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                通常在使用 mockito 時,我會做類似的事情

                Normally when using mockito I will do something like

                Mockito.when(myObject.myFunction(myParameter)).thenReturn(myResult);
                

                有沒有可能做一些類似的事情

                Is it possible to do something along the lines of

                myParameter.setProperty("value");
                Mockito.when(myObject.myFunction(myParameter)).thenReturn("myResult");
                
                myParameter.setProperty("otherValue");
                Mockito.when(myObject.myFunction(myParameter)).thenReturn("otherResult");
                

                所以而不是僅僅使用參數來確定結果.它使用參數內的屬性值來確定結果.

                So rather than when just using the parameter to determine the result. It is using a value of a property inside the parameter to determine the result.

                所以當代碼執行時,它的行為是這樣的

                so when the code is executed it behaves like so

                public void myTestMethod(MyParameter myParameter,MyObject myObject){
                    myParameter.setProperty("value");
                    System.out.println(myObject.myFunction(myParameter));// outputs myResult
                
                    myParameter.setProperty("otherValue");
                    System.out.println(myObject.myFunction(myParameter));// outputs otherResult
                }
                

                <小時>

                目前的解決方案,希望可以提出更好的建議.


                current solution, hopefully something better can be suggested.

                private class MyObjectMatcher extends ArgumentMatcher<MyObject> {
                
                    private final String compareValue;
                
                    public ApplicationContextMatcher(String compareValue) {
                        this.compareValue= compareValue;
                    }
                
                    @Override
                    public boolean matches(Object argument) {
                        MyObject item= (MyObject) argument;
                        if(compareValue!= null){
                            if (item != null) {
                                return compareValue.equals(item.getMyParameter());
                            }
                        }else {
                            return item == null || item.getMyParameter() == null;
                        }
                        return false;
                    }
                }
                
                public void initMock(MyObject myObject){
                    MyObjectMatcher valueMatcher = new MyObjectMatcher("value");
                    MyObjectMatcher otherValueMatcher = new MyObjectMatcher("otherValue");
                    Mockito.when(myObject.myFunction(Matchers.argThat(valueMatcher))).thenReturn("myResult");
                    Mockito.when(myObject.myFunction(Matchers.argThat(otherValueMatcher))).thenReturn("otherResult");
                }
                

                推薦答案

                這是一種方法.這使用 Answer 對象來檢查屬性的值.

                Here's one way of doing it. This uses an Answer object to check the value of the property.

                @RunWith(MockitoJUnitRunner.class)
                public class MyTestClass {
                    private String theProperty;
                    @Mock private MyClass mockObject;
                
                    @Before
                    public void setUp() {
                        when(mockObject.myMethod(anyString())).thenAnswer(
                            new Answer<String>(){
                            @Override
                            public String answer(InvocationOnMock invocation){
                                if ("value".equals(theProperty)){
                                    return "result";
                                }
                                else if("otherValue".equals(theProperty)) {
                                    return "otherResult";
                                }
                                return theProperty;
                            }});
                    }
                }
                

                還有一種我更喜歡的替代語法,它可以實現完全相同的效果.由你選擇其中的哪一個.這只是 setUp 方法 - 測試類的其余部分應該與上面相同.

                There's an alternative syntax, which I actually prefer, which will achieve exactly the same thing. Over to you which one of these you choose. This is just the setUp method - the rest of the test class should be the same as above.

                @Before
                public void setUp() {
                    doAnswer(new Answer<String>(){
                        @Override
                        public String answer(InvocationOnMock invocation){
                            if ("value".equals(theProperty)){
                                return "result";
                            }
                            else if("otherValue".equals(theProperty)) {
                                return "otherResult";
                            }
                            return theProperty;
                        }}).when(mockObject).myMethod(anyString());
                }
                

                這篇關于基于參數屬性的模擬返回值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='AMQfO'></small><noframes id='AMQfO'>

                  <tbody id='AMQfO'></tbody>

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

                      • <tfoot id='AMQfO'></tfoot>
                          <bdo id='AMQfO'></bdo><ul id='AMQfO'></ul>
                          主站蜘蛛池模板: 国产美女在线免费观看 | 伊人网伊人网 | 国产精品视频999 | 欧美精品一区二区免费视频 | 妹子干综合 | 国产精品视频偷伦精品视频 | 日韩欧美视频在线 | 中文字幕91 | 九九亚洲 | 日韩二区| 韩国欧洲一级毛片 | 精品免费观看 | 国产精品视频一区二区三区不卡 | 欧美久久一区二区 | 国产精品亚洲一区二区三区在线 | 中文字幕国产一区 | 一区二区在线看 | 欧美激情在线精品一区二区三区 | 日韩欧美亚洲 | 九九热免费看 | 91看片网 | 99热首页| 在线播放中文字幕 | 毛片链接 | 色伊人久久 | 久操伊人 | 国产精品视频免费 | 人人草人人干 | 在线看成人av| 日本五月婷婷 | 国产1区2区| 国产黄色大片 | 一区二区三区国产 | 日韩欧美在线视频观看 | www.日本三级 | 国产男人的天堂 | 久久精品影视 | 久久久国产精品 | avmans最新导航地址 | 午夜tv免费观看 | 超碰免费在线 |