Skip to content

AbstractRequestExpectationManager fails with "Expectations already declared" when ResponseCreator.createResponse throws an exception [SPR-16132] #20680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Oct 30, 2017 · 2 comments
Assignees
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

Comments

@spring-projects-issues
Copy link
Collaborator

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

public void makeServiceCall(RestTemplate restTemplate) {
  try {
    restTemplate.getForEntity("/some-service/some-endpoint", String.class);
  }
  catch (Exception e) {
    restTemplate.postForEntity("/reporting-service/report-error", e.toString(), String.class);
  }
}

Test that reproduces the failure:

  @Test
  public void testRestControllerHandlesMessage_FailsWithUnknownException() throws InterruptedException {
    RestTemplate restTemplate = new RestTemplate();
    MockRestServiceServer mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build();
    mockRestServiceServer
        .expect(requestTo("/some-service/some-endpoint"))
        .andRespond((request) -> { throw new IllegalStateException("pseudo network error"); });
    mockRestServiceServer
        .expect(requestTo("/reporting-service/report-error"))
        .andExpect(method(POST))
        .andRespond(withSuccess());

    makeServiceCall(restTemplate);
    mockRestServiceServer.verify();
  }

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

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Seems we've been working on this in parallel... I'll rearrange my few further changes around your pull request.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I've applied a few refinements to master, also adding a MockRestServiceServer level test. I'll backport the overall change to 4.3.13 tomorrow. Thanks for raising this!

@spring-projects-issues spring-projects-issues added type: bug A general bug in: test Issues in the test module status: backported An issue that has been backported to maintenance branches labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0.2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants