MockRestServiceServer simulate backend timeout in integration test

前端 未结 5 1911
太阳男子
太阳男子 2021-02-12 20:02

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

5条回答
  •  春和景丽
    2021-02-12 20:36

    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);
        });
    

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

提交回复
热议问题