Skip to content

MockRestServiceServerBuilder cannot be cached [SPR-14306] #18878

@spring-projects-issues

Description

@spring-projects-issues

Stéphane Nicoll opened SPR-14306 and commented

I am investigating the option of adding a @RestClientTest annotation in Spring Boot that would automatically configure a RestTemplate and MockRestServiceServer.

The default expectation managers throw an exception if you try to add a request after you have validated one, something like:

java.lang.IllegalStateException: Cannot add more expectations after actual requests are made.

	at org.springframework.util.Assert.state(Assert.java:392)
	at org.springframework.test.web.client.AbstractRequestExpectationManager.expectRequest(AbstractRequestExpectationManager.java:60)
	at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:112)
	at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:95)

There is no way to "reset" the mock server at the end of the test so it looks like the only way to make this work is to create a new server instance per test.

My next move was to try to share the builder and simply call build in a Before annotated method but that does not work either as the builder shares an expectation manager by default, which means that every created server using the same builder share the same expectation manager.

Looking at the API, I find it weird that we can both use ignoreExpectOrder and expectationManager as those are mutually exclusive. Perhaps removing the latter and creating a fresh expectation manager would help? It also would be nice if a server instance could be reused for multiple tests with a public reset method that would reset the expectation manager.


Affects: 4.3 RC2

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions