• <small id='6cVOu'></small><noframes id='6cVOu'>

    1. <tfoot id='6cVOu'></tfoot>

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

        如何使用 MockRestServiceServer 模拟 RestTemplate?

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

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

                • <bdo id='i6JD8'></bdo><ul id='i6JD8'></ul>

                • <legend id='i6JD8'><style id='i6JD8'><dir id='i6JD8'><q id='i6JD8'></q></dir></style></legend>
                    <tbody id='i6JD8'></tbody>
                  <i id='i6JD8'><tr id='i6JD8'><dt id='i6JD8'><q id='i6JD8'><span id='i6JD8'><b id='i6JD8'><form id='i6JD8'><ins id='i6JD8'></ins><ul id='i6JD8'></ul><sub id='i6JD8'></sub></form><legend id='i6JD8'></legend><bdo id='i6JD8'><pre id='i6JD8'><center id='i6JD8'></center></pre></bdo></b><th id='i6JD8'></th></span></q></dt></tr></i><div id='i6JD8'><tfoot id='i6JD8'></tfoot><dl id='i6JD8'><fieldset id='i6JD8'></fieldset></dl></div>
                  本文介绍了如何使用 MockRestServiceServer 模拟 RestTemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  @RunWith(MockitoJUnitRunner.class)
                  public class FeatureFlipperManagerTest {
                      @Autowired
                      RestTemplate restTemplate = new RestTemplate();
                      @Autowired
                      Service service = new Service();
                      MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
                  
                      @Test
                      public void test() throws Exception {
                        mockServer.expect(requestTo(Mockito.anyString()))
                                  .andRespond(withSuccess("{"enabled":true}", MediaType.APPLICATION_JSON));
                          boolean res = service.isEnabled("xxx");
                          mockServer.verify();
                          Assert.assertEquals(true, res);
                      }
                  }
                  

                  我有 MockRestServiceServer 在服务中模拟 restTemplete.但它总是失败.它将错误显示为 java.lang.AssertionError: Additional request(s) expected1 个中有 0 个被执行.任何人都可以让我知道我哪里做得不对.

                  I have MockRestServiceServer to mock restTemplete in a service. But it always fail. it shows the error as java.lang.AssertionError: Further request(s) expected 0 out of 1 were executed. Any one could let me know where I did not do it right.

                  服务本身将如下所示:

                  public class Service{
                      public boolean isEnabled(String xxx) {
                          RestTemplate restTemplate = new RestTemplate();
                          ResponseEntity<String> response = restTemplate.getForEntity("someurl",String.class);
                          if(...)return true;
                          return false;
                      }
                  }
                  

                  推荐答案

                  首先,您的 Service 类在每次请求时都会创建一个新的 RestTemplate 实例.我不能强调这是多么糟糕的做法.创建一个 RestTemplate 类型的 bean 并将其注入您的 Service bean(它很可能已经创建 - 取决于您使用的 Spring MVC 版本).

                  First of all, your Service class creates a new instance of RestTemplate on every request. I cannot stress enough how bad practice it is. Create a bean of type RestTemplate and inject it into your Service bean (it is most likely already created - depending on the Spring MVC version you are using).

                  一旦你有了它,那么两个 RestTemplates:一个在你的 Service bean 和一个注入到 FeatureFlipperManagerTest 将是相同的,并使用 MockRestServiceServer 将是可能的.

                  Once you have it, then both RestTemplates: one in your Service bean and one injected into FeatureFlipperManagerTest will be the same and testing with MockRestServiceServer will be possible.

                  编辑 - 更明确地说:

                  EDIT - to be more explicit:

                  将您的 Service 类修改为:

                  @Component
                  public class Service {
                  
                      private RestTemplate restTemplate;  
                  
                      @Autowired 
                      public Service(RestTemplate restTemplate) {
                          this.restTemplate = restTemplate;
                      }
                  
                      public boolean isEnabled(String xxx) {
                          ResponseEntity<String> response = restTemplate.getForEntity("someurl",String.class);
                          if(...)return true;
                          return false;
                      }
                  }
                  

                  和你的测试类:

                  @RunWith(MockitoJUnitRunner.class)
                  public class FeatureFlipperManagerTest {
                      @Autowired
                      RestTemplate restTemplate;
                  
                      @Autowired
                      Service service;
                  
                      MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
                  
                      @Test
                      public void test() throws Exception {
                        mockServer.expect(requestTo(Mockito.anyString()))
                                  .andRespond(withSuccess("{"enabled":true}", MediaType.APPLICATION_JSON));
                          boolean res = service.isEnabled("xxx");
                          mockServer.verify();
                          Assert.assertEquals(true, res);
                      }
                  }
                  

                  如果这失败并出现异常说没有 RestTemplate bean 存在,那么请粘贴有关您正在使用的 Spring(Spring Boot?)版本的信息.

                  If this fails with exception saying that there is no RestTemplate bean present then please paste info about version of Spring (Spring Boot?) you are using.

                  这篇关于如何使用 MockRestServiceServer 模拟 RestTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:当父级在不同的包中时模拟受保护的父级方法 下一篇:Junit 5 找实际环境配置运行测试

                  相关文章

                  最新文章

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

                    <small id='2wYhi'></small><noframes id='2wYhi'>

                    <legend id='2wYhi'><style id='2wYhi'><dir id='2wYhi'><q id='2wYhi'></q></dir></style></legend>