1. <small id='AL1zV'></small><noframes id='AL1zV'>

  2. <tfoot id='AL1zV'></tfoot>
      <bdo id='AL1zV'></bdo><ul id='AL1zV'></ul>
    <i id='AL1zV'><tr id='AL1zV'><dt id='AL1zV'><q id='AL1zV'><span id='AL1zV'><b id='AL1zV'><form id='AL1zV'><ins id='AL1zV'></ins><ul id='AL1zV'></ul><sub id='AL1zV'></sub></form><legend id='AL1zV'></legend><bdo id='AL1zV'><pre id='AL1zV'><center id='AL1zV'></center></pre></bdo></b><th id='AL1zV'></th></span></q></dt></tr></i><div id='AL1zV'><tfoot id='AL1zV'></tfoot><dl id='AL1zV'><fieldset id='AL1zV'></fieldset></dl></div>

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

      用 Mockito 模拟重载的方法

      时间:2023-09-26

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

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

          • <bdo id='HOo7H'></bdo><ul id='HOo7H'></ul>
            <tfoot id='HOo7H'></tfoot>
                <tbody id='HOo7H'></tbody>
              • 本文介绍了用 Mockito 模拟重载的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在测试一些依赖于 RestTemplate 类中的 getForObject() 方法的方法.

                I am testing some methods that rely on the getForObject() method in the RestTemplate class.

                getForObject() 方法重载了签名getForObject(String url, Class responseType, Object... uriVariables)getForObject(字符串 url, 类 responseType, Map

                The getForObject() method is overloaded with the signaturesgetForObject(String url, Class<T> responseType, Object... uriVariables) and getForObject(String url, Class<T> responseType, Map<String, ?>

                我需要在其参数中使用 Object... 存根方法以引发异常,但我不能因为 Mockito.any() 还包含 地图类型.因此,将方法存根为 getForObject(Mockito.anyString(),Mockito.any(), Mockito.any() 将指向触发编译错误的两个方法.

                I need to stub the method with Object... in its arguments to throw an exception but I can not because Mockito.any() also encompasses the Map type. Therefore, stubbing the method as getForObject(Mockito.anyString(),Mockito.any(), Mockito.any() will point to BOTH methods triggering a compilation error.

                这个问题有什么可能的解决方法吗?

                Are there any possible workarounds to this problem?

                我已经尝试使用 Mockito.anyObject() 无济于事

                I have already tried using Mockito.anyObject() to no avail

                推荐答案

                不确定你的问题可能是什么,但在这一点上,我不妨发布一个工作示例.

                Not sure what your problem might be, but at this point I might as well just post a working example.

                如前所述,您需要正确指定每个参数的类型,以便 mockito 可以定位到匹配的方法签名.

                As mentioned before you need to properly specify the type of each parameter, so that mockito can locate the matching method signature.

                有关处理旧 mockito 版本使用的可变参数的语法,请查看 这个答案.

                For the syntax to handle varargs used by older mockito versions, check this answer.

                import static org.mockito.ArgumentMatchers.any;
                ...
                
                @RunWith(MockitoJUnitRunner.class)
                public class MockitoTest {
                
                    @Test
                    public void test() throws Exception {
                
                        RestTemplate api = Mockito.mock(RestTemplate.class);
                
                        Object obj1 = new Object();
                        Object obj2 = new Object();
                        Object obj3 = new Object();
                
                        Mockito.when(api.getForObject(any(String.class),any(Class.class), ArgumentMatchers.<Object>any())).thenReturn(obj1);
                        Mockito.when(api.getForObject(any(String.class),any(Class.class), any(Map.class))).thenReturn(obj2);
                        Mockito.when(api.getForObject(any(URI.class),any(Class.class))).thenReturn(obj3);
                
                        Assert.assertEquals(obj1, api.getForObject("", String.class));
                        Assert.assertEquals(obj1, api.getForObject("", String.class, obj1));
                        Assert.assertEquals(obj1, api.getForObject("", String.class, obj1, obj2));
                        Assert.assertEquals(obj1, api.getForObject("", String.class, obj1, obj2, obj3));
                        Assert.assertEquals(obj1, api.getForObject("", String.class, new Object[] {obj1,obj2,obj3}));
                
                        Assert.assertEquals(obj2, api.getForObject("", String.class, new HashMap()));
                
                        Assert.assertEquals(obj3, api.getForObject(new URI(""), String.class));
                    }
                }
                

                对于您的用例,只需将 thenReturn 替换为 thenThrow.

                For your usecase just replace the thenReturn with thenThrow.

                这篇关于用 Mockito 模拟重载的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <bdo id='fTNrC'></bdo><ul id='fTNrC'></ul>

                    <tbody id='fTNrC'></tbody>
                  <legend id='fTNrC'><style id='fTNrC'><dir id='fTNrC'><q id='fTNrC'></q></dir></style></legend><tfoot id='fTNrC'></tfoot>
                • <small id='fTNrC'></small><noframes id='fTNrC'>

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