Search code examples
spring-bootintegration-testingspring-boot-testwiremockspring-cloud-contract

Using Spring Cloud Contract Stub Runner with manually created stubs


I'm not sure whether using spring-cloud-contract for the purpose which I mentioned in the title is right or not but I want to understand the use case of it in such scenario.

We are updating our integration tests to use Spring Cloud Contract. For the new features, we are following the documentation for creating stubs from YAML/Groovy and then using them in consumer side with spring-cloud-starter-contract-stub-runner.

My concern is the stubs which were already written and being used. We don't want to spend time on re-writing them in Groovy/YAML to make Spring Cloud Contract to generate stubs as we already have the stubs. Existing test configuration is as follows:

@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    classes = Application.class
)
@AutoConfigureWireMock(port = 8989, stubs = "classpath*:**/stubs/mappings/**/*.json", files = "classpath*:**/stubs")
class MyClientIT { ... }

When it comes to changing this test to use spring-cloud-contract, I'm stuck with how to configure stub-runner:

@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    classes = Application.class
)
@AutoConfigureMockMvc
@AutoConfigureStubRunner(???)
public class MyNewClientIT {...}

Is it possible and advised to use Spring Cloud Contract for such a case? And if so, how can we make stub-runner to see locally stored (not stored as an artefact locally or remotely) stubs?

Thank you...


Solution

  • The easiest way will be to take the stubs and re-package them to follow the META-INF/group.id/artifactid/mappings/... folder structure. Then you can use the Stub Runner classpath mode to pick those stubs via ids.