I’m working on integration tests for my REST controller and using MockRestServiceServer to mock the backend calls. Right now I need to test how my app handles slow backend responses that eventually timeout.
I want to make the mocked response take a really long time so my application will timeout. Is there a way to add delay or latency to MockRestServiceServer responses? Can I configure the mock server itself to be slow? Or should I look into other tools like WireMock instead?
MockRestServiceServer isn’t ideal for timeout testing due to its synchronous execution on the same thread. While you could implement a custom ResponseCreator with delay logic, I would recommend considering WireMock for better functionality in this regard. WireMock allows you to easily use .withFixedDelay(milliseconds), eliminating any threading complications. If you must utilize MockRestServiceServer, ensure your RestTemplate is correctly configured with appropriate timeout settings; otherwise, your intended delays may not effectively trigger the timeouts you’re aiming to test.
mockrestserviceServer’s pretty limited for this stuff. just use wiremock instead - it’s way easier to simulate delays without dealing with threads. i’ve tried timeout tests with mockrestserviceserver and it’s not worth the hassle.
interesting challenge! you could try Thread.sleep() in a custom ResponseCreator - something like andRespond(response -> { Thread.sleep(30000); return withSuccess(...); }). what timeout values are you testing against? and is your restTemplate timeout config set up right for these tests?