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:

https://github.com/spring-cloud-samples/spring-cloud-contract-samples/issues/106

Comment From: rstoyanchev

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.