Skip to content
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

Add an MockMVC alwaysDo equivalent to WebTestClient #26662

Closed
marcingrzejszczak opened this issue Mar 11, 2021 · 1 comment
Closed

Add an MockMVC alwaysDo equivalent to WebTestClient #26662

marcingrzejszczak opened this issue Mar 11, 2021 · 1 comment
Assignees
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@marcingrzejszczak
Copy link
Contributor

marcingrzejszczak commented Mar 11, 2021

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

@rstoyanchev
Copy link
Contributor

The challenge is that the body could be decoded in different ways, e.g. to a byte[], to some Object of type T or List<T>, or also to Flux<T> for external consumption via StepVerifier. We could provide a contract with hooks into all these ways and invoke the appropriate callback depending on what the test does. I'll experiment to see what comes out.

@rstoyanchev rstoyanchev added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 11, 2021
@rstoyanchev rstoyanchev self-assigned this Mar 11, 2021
@rstoyanchev rstoyanchev added this to the 5.3.5 milestone Mar 11, 2021
This was referenced Mar 17, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
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 in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants