Skip to content

Add an MockMVC alwaysDo equivalent to WebTestClient  #26662

Closed
@marcingrzejszczak

Description

@marcingrzejszczak

In Spring Cloud Contract we're generating tests for our users. Typically what is being used is the RestAssured library for sending out HTTP requests. RestAssured has a MockMVC and WebTestClient extensions. The users can't modify the generated tests - what they can do is to write code in the base class that the generated test extends.

With MockMVC I could do the following

@ExtendWith(RestDocumentationExtension.class)
public abstract class BeerRestBase {
	ProducerController producerController = new ProducerController(oldEnough());
	StatsController statsController = new StatsController(statsService());

	private PersonCheckingService oldEnough() {
		return argument -> argument.age >= 20;
	}

	private StatsService statsService() {
		return name -> new Random().nextInt();
	}

	@BeforeEach
	public void setup(RestDocumentationContextProvider provider, TestInfo testInfo) {
		RestAssuredMockMvc.mockMvc(MockMvcBuilders.standaloneSetup(this.producerController, this.statsController)
				.apply(documentationConfiguration(provider))
				.alwaysDo(document(getClass().getSimpleName() + "_" + testInfo.getDisplayName()))
				.build());
	}
}

Notice the .alwaysDo(document(getClass().getSimpleName() + "_" + testInfo.getDisplayName())) part. We are running this assertion for each test method and each MockMVC call.

I'd like to achieve sth similar for WebTestClient.

I can write this:

WebTestClient webTestClient = WebTestClient.bindToController(this.producerController, this.statsController).configureClient()
				.filter(documentationConfiguration(provider))
				.build();
RestAssuredWebTestClient.webTestClient(webTestClient);

This is a fine way of hooking RestDocs to WebTestClient. I can't however make the global consumeWith(...) call like in the RestDocs snippet

this.webTestClient.get().uri("/").accept(MediaType.APPLICATION_JSON) 
		.exchange().expectStatus().isOk() 
		.expectBody().consumeWith(document("index")); 

It would be great to add sch a functionality.

cc @wilkinsona

Related issue:

spring-cloud-samples/spring-cloud-contract-samples#106

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