AbstractRequestExpectationManager fails with "Expectations already declared" when ResponseCreator.createResponse throws an exception [SPR-16132] #20680
Labels
in: test
Issues in the test module
status: backported
An issue that has been backported to maintenance branches
type: bug
A general bug
Milestone
Eric Pabst opened SPR-16132 and commented
I am trying to test a scenario where an HTTP request fails without getting a response such as a network error, and that failure is handled by making another RestTemplate request. When I provide a ResponseCreator that throws an exception in createResponse, the test fails with "IllegalStateException: Expectations already declared".
(Over-simplified) Code under test
Test that reproduces the failure:
I debugged this and found out that AbstractRequestExpectationManager.validateRequest only adds the request to the requests collection after validateRequestInternal returns normally. Then when validateRequest is called again for the 2nd RestTemplate call it fails because requests is still empty.
All that is needed is to put "requests.add(request);" into a finally block.
Affects: 4.3.12, 5.0.1
Referenced from: pull request #1580, and commits a88c47a, 43d88e4, 295e3b6, 3c07afc
Backported to: 4.3.13
The text was updated successfully, but these errors were encountered: