wiremock / wiremock-spring-boot

WireMock Spring Boot drastically simplifies testing HTTP clients in Spring Boot & Junit 5 based integration tests.
MIT License
292 stars 16 forks source link

Load json mocks from dynamically defined directories #57

Open XakepSDK opened 3 days ago

XakepSDK commented 3 days ago

Proposal

We have a per-test directory for mocks. In resources, we have a such hierarchy: mocks/{TestClassName}/{TestMethodName}/my-mock.json

Each mock file URL starts with a service name prefix which is taken from ConfigureWireMock#name property. This way mocks are loaded in to the correct WireMockServer instance.

If tests need something common, then you can put your mocks in a directory higher in hierarchy. If you put your mocks in mocks/{TestClassName} then those mocks will be loaded for all tests inside TestClassName. You can put mocks in mocks if you need something to be available in all tests.

The way I match mocks to the correct WireMockServer might be unfeasible for some users. I guess they could put their mocks in another subdirectory, e.g mocks/{TestClassName}/{TestMethodName}/{ServerName}/my-mock.json or name their mock files as {ServerName}.json.

Would be nice to have something like this built-in in this extension. Currently I'm just hacking in Store, get WireMockServer and inject or clean-up stuff if I need. All of this is done in a custom BeforeTestExecutionCallback

p.s. ClasspathFileSource might be a bottleneck for this use-case. In our implementation, I'm thinking about preloading all stubs in a in-memory store and load from it when needed.

References

No response

tomasbjerre commented 3 days ago

How would you like to configure it?

Im thinking it can be done with an option to enable/disable loading mocks from all specified directories. And another one for all specified classpath paths. False by default.

Or perhaps have an option that specifies how many parents of a specified directory, or classpath resource, should also be loaded. Default would be 0.

If that would solve your issue?