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

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

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

    <tfoot id='b1GHe'></tfoot>

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

      模棱两可的方法调用模拟 RestTemplate.exchange()

      时间:2023-09-25
      <tfoot id='2QdPS'></tfoot>

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

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

                本文介绍了模棱两可的方法调用模拟 RestTemplate.exchange()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                无法找出使用匹配器来识别我所针对的交换方法的重载的正确方法.我正在拨打的电话:

                Can't figure out the correct way to use matchers to identify which overload of the exchange method I am targetting. The call I am making:

                restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Object.class)

                我尝试过使用 any(Class.class) 和其他一些东西,但没有任何效果.我试图区分两种具有相似签名的方法:

                I've tried using any(Class.class), and a couple other things but nothing is working. There are 2 methods with a similar signature that I am trying to distinguish between:

                exchange(String url, HttpMethod 方法, @Nullable HttpEntity<?> requestEntity, Class<T> responseType)

                exchange(String var1, HttpMethod var2, @Nullable HttpEntity

                这是我目前与 Mockito 相关的导入:

                Here are my current imports related to Mockito:

                import org.mockito.Mock;
                import org.mockito.MockitoAnnotations;
                
                import static org.junit.Assert.*;
                import static org.mockito.ArgumentMatchers.any;
                import static org.mockito.ArgumentMatchers.anyString;
                import static org.mockito.Mockito.*;
                

                是否有人能够模拟对使用 Class 作为第四个参数而不是 ParameterizedTypeReference 的方法的调用?

                Has anyone been able to mock a call to this method that uses a Class as the 4th parameter instead of a ParameterizedTypeReference?

                推荐答案

                我不确定是我误解了你的问题还是 @MarciejKowalski 提到的问题,但是从问题运行测试时或我认为与您针对 mockito-core-2.23.4/JDK 1.8.0_151 的示例类似,它工作得很好.

                I am not sure whether I misunderstood your question or the issue mentioned by @MarciejKowalski, but when running the test from the issue or what I suppose is similar to your example against mockito-core-2.23.4 / JDK 1.8.0_151 it works just fine.

                [我在您的示例中使用了 JUnit 4 而不是 JUnit 5]

                [I used JUnit 4 for your example instead of JUnit 5]

                import static org.mockito.ArgumentMatchers.any;
                
                import org.junit.Assert;
                import org.junit.Test;
                import org.junit.runner.RunWith;
                import org.mockito.Mockito;
                import org.mockito.junit.MockitoJUnitRunner;
                import org.springframework.core.ParameterizedTypeReference;
                import org.springframework.http.HttpEntity;
                import org.springframework.http.HttpMethod;
                import org.springframework.http.ResponseEntity;
                import org.springframework.web.client.RestTemplate;
                
                @RunWith(MockitoJUnitRunner.class)
                public class MockitoTest {
                
                    @Test
                    public void test() {
                
                        RestTemplate api = Mockito.mock(RestTemplate.class);
                        ResponseEntity<?> response1 = Mockito.mock(ResponseEntity.class);
                        ResponseEntity<?> response2 = Mockito.mock(ResponseEntity.class);
                
                        Mockito.when(api.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class))).thenReturn(response1);
                        Mockito.when(api.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(ParameterizedTypeReference.class))).thenReturn(response2);
                
                        ParameterizedTypeReference mock = Mockito.mock(ParameterizedTypeReference.class);
                
                        Assert.assertEquals(response1, api.exchange("", HttpMethod.GET, new HttpEntity(""), String.class));
                        Assert.assertEquals(response2, api.exchange("", HttpMethod.GET, new HttpEntity(""), mock));
                    }
                }
                

                这篇关于模棱两可的方法调用模拟 RestTemplate.exchange()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <tbody id='LVGTY'></tbody>

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

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

                1. <legend id='LVGTY'><style id='LVGTY'><dir id='LVGTY'><q id='LVGTY'></q></dir></style></legend>

                        <bdo id='LVGTY'></bdo><ul id='LVGTY'></ul>
                      • <tfoot id='LVGTY'></tfoot>