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

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

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

    1. MockRestServiceServer 在集成测试中模拟后端超时

      时间:2023-10-01
        <tfoot id='jewCM'></tfoot>
            <tbody id='jewCM'></tbody>
          • <i id='jewCM'><tr id='jewCM'><dt id='jewCM'><q id='jewCM'><span id='jewCM'><b id='jewCM'><form id='jewCM'><ins id='jewCM'></ins><ul id='jewCM'></ul><sub id='jewCM'></sub></form><legend id='jewCM'></legend><bdo id='jewCM'><pre id='jewCM'><center id='jewCM'></center></pre></bdo></b><th id='jewCM'></th></span></q></dt></tr></i><div id='jewCM'><tfoot id='jewCM'></tfoot><dl id='jewCM'><fieldset id='jewCM'></fieldset></dl></div>

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

            1. <legend id='jewCM'><style id='jewCM'><dir id='jewCM'><q id='jewCM'></q></dir></style></legend>
                <bdo id='jewCM'></bdo><ul id='jewCM'></ul>
              • 本文介绍了MockRestServiceServer 在集成测试中模拟后端超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 MockRestServiceServer 在我的 REST 控制器上编写某种集成测试来模拟后端行为.我现在想要实现的是模拟来自后端的非常慢的响应,这最终会导致我的应用程序超时.似乎可以使用 WireMock 实现,但目前我想坚持使用 MockRestServiceServer.

                I am writing some kind of integration test on my REST controller using MockRestServiceServer to mock backend behaviour. What I am trying to achieve now is to simulate very slow response from backend which would finally lead to timeout in my application. It seems that it can be implemented with WireMock but at the moment I would like to stick to MockRestServiceServer.

                我正在创建这样的服务器:

                I am creating server like this:

                myMock = MockRestServiceServer.createServer(asyncRestTemplate);
                

                然后我在嘲笑我的后端行为:

                And then I'm mocking my backend behaviour like:

                myMock.expect(requestTo("http://myfakeurl.blabla"))
                            .andExpect(method(HttpMethod.GET))
                            .andRespond(withSuccess(myJsonResponse, MediaType.APPLICATION_JSON));
                

                是否可以为响应添加某种延迟或超时或其他类型的延迟(或者可能是整个模拟服务器,甚至是我的 asyncRestTemplate)?或者我应该切换到 WireMock 还是 Restito?

                Is it possible to add some kind of a delay or timeout or other kind of latency to the response (or maybe whole mocked server or even my asyncRestTemplate)? Or should I just switch to WireMock or maybe Restito?

                推荐答案

                你可以这样实现这个测试功能(Java 8):

                You can implement this test functionality this way (Java 8):

                myMock
                    .expect(requestTo("http://myfakeurl.blabla"))
                    .andExpect(method(HttpMethod.GET))
                    .andRespond(request -> {
                        try {
                            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
                        } catch (InterruptedException ignored) {}
                        return new MockClientHttpResponse(myJsonResponse, HttpStatus.OK);
                    });
                

                但是,我应该警告您,由于 MockRestServiceServer 只是替换了 RestTemplate requestFactory,因此您所做的任何 requestFactory 设置都将在测试环境中丢失.

                But, I should warn you, that since MockRestServiceServer simply replaces RestTemplate requestFactory any requestFactory settings you'd make will be lost in test environment.

                这篇关于MockRestServiceServer 在集成测试中模拟后端超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:doThrow 中看起来正确的 Mockito 异常 下一篇:Mockito、JUnit、Hamcrest、版本控制

                相关文章

                最新文章

              • <small id='2QyyK'></small><noframes id='2QyyK'>

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

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