Consistent API for defining expectations. #293

Closed
opened 2025-12-29 08:25:33 +01:00 by adam · 8 comments
Owner

Originally created by @cezarypiatekGC on GitHub (Sep 11, 2020).

Hi,

I would like to use WireMock.Net in my project but I need to handle the following scenario. When we run tests locally (in VisualStudio) then we want to run WireMock in memory using WireMockServer.Start. When we run tests in CI then we want to use docker container with WireMock and connect to it from the test code and from the tested application. Besides that difference, we would like to have a consistent API to define mocking rules. However, it doesn't seem to be possible right now because WireMock client uses different models to define mappings (MappingModel) and the MappingConverter which could convert IMapping to MappingModel has an internal modifier. Is it possible to expose the method which could perform that conversion? Or am I doing something wrong and there's an option to define Mappings once and use it with WireMockServer and with standalone (docker) app? Ofc, WireMockServer has WithMapping method which accepts MappingModel but creating MappingModel manually seems to be a very tedious task. I Would prefer to use that fluent interface with Request.Create() and Response.Create()

Originally created by @cezarypiatekGC on GitHub (Sep 11, 2020). Hi, I would like to use WireMock.Net in my project but I need to handle the following scenario. When we run tests locally (in VisualStudio) then we want to run WireMock in memory using WireMockServer.Start. When we run tests in CI then we want to use docker container with WireMock and connect to it from the test code and from the tested application. Besides that difference, we would like to have a consistent API to define mocking rules. However, it doesn't seem to be possible right now because WireMock client uses different models to define mappings (MappingModel) and the MappingConverter which could convert IMapping to MappingModel has an internal modifier. Is it possible to expose the method which could perform that conversion? Or am I doing something wrong and there's an option to define Mappings once and use it with WireMockServer and with standalone (docker) app? Ofc, WireMockServer has WithMapping method which accepts MappingModel but creating MappingModel manually seems to be a very tedious task. I Would prefer to use that fluent interface with Request.Create() and Response.Create()
adam added the question label 2025-12-29 08:25:33 +01:00
adam closed this issue 2025-12-29 08:25:33 +01:00
Author
Owner

@StefH commented on GitHub (Sep 11, 2020):

Hello @cezarypiatekGC,

For your scenario I would recommend to use (static) mapping files to be used in your local unit-tests and by your WireMock.net running in a docker container.

1. Create a mapping.json file

For the format, see https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#post---__adminmappings

And place this file in the __admin/mappings folder.

2. Now in your unit tests, you can use:

var server = WireMockServer.Start(new WireMockServerSettings
{
  ReadStaticMappings = true,
  WatchStaticMappings = true,
  WatchStaticMappingsInSubdirectories = true
};

The unit tests will use the mappings defined in the mapping.json file.

See also https://github.com/WireMock-Net/WireMock.Net/wiki/Settings#readstaticmappings

3. Docker

Getting this to work in docker container is not 100% easy supported because loading that mapping.json file can not be done at startup because the docker image is already created.

Some options what you could do is after the docker container has been started, just use the admin rest interface to post the mapping.json content.
You can just use any tool to do a http post to the /__admin endpoint, or use the c# client api:
https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#client-api

@StefH commented on GitHub (Sep 11, 2020): Hello @cezarypiatekGC, For your scenario I would recommend to use (static) mapping files to be used in your local unit-tests and by your WireMock.net running in a docker container. ## 1. Create a mapping.json file For the format, see https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#post---__adminmappings And place this file in the **__admin/mappings** folder. ## 2. Now in your unit tests, you can use: ``` c# var server = WireMockServer.Start(new WireMockServerSettings { ReadStaticMappings = true, WatchStaticMappings = true, WatchStaticMappingsInSubdirectories = true }; ``` The unit tests will use the mappings defined in the mapping.json file. See also https://github.com/WireMock-Net/WireMock.Net/wiki/Settings#readstaticmappings ## 3. Docker Getting this to work in docker container is not 100% easy supported because loading that mapping.json file can not be done at startup because the docker image is already created. Some options what you could do is after the docker container has been started, just use the admin rest interface to post the mapping.json content. You can just use any tool to do a http post to the /__admin endpoint, or use the c# client api: https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#client-api
Author
Owner

@StefH commented on GitHub (Sep 11, 2020):

Or, you can also just run a console app instead of a docker image. See https://github.com/HeadsInsane/Challenge/tree/master/src/BankSimulator and the related main project on how it's also possible.

@StefH commented on GitHub (Sep 11, 2020): Or, you can also just run a console app instead of a docker image. See https://github.com/HeadsInsane/Challenge/tree/master/src/BankSimulator and the related main project on how it's also possible.
Author
Owner

@cezarypiatekGC commented on GitHub (Sep 11, 2020):

Hi @StefH
Thanks for your response. Is there any reason that prevents you from exposing MappingConverter? That would simply a lot.

@cezarypiatekGC commented on GitHub (Sep 11, 2020): Hi @StefH Thanks for your response. Is there any reason that prevents you from exposing `MappingConverter`? That would simply a lot.
Author
Owner

@StefH commented on GitHub (Sep 11, 2020):

There is no real reason, except that I'd like to expose only the classes which are really used by the users.

But if it helps you, I can make this class public. And add a comment like This class is used by WireMock.Net internally. It isn't intended for use in application code.

@StefH commented on GitHub (Sep 11, 2020): There is no real reason, except that I'd like to expose only the classes which are really used by the users. But if it helps you, I can make this class public. And add a comment like `This class is used by WireMock.Net internally. It isn't intended for use in application code.`
Author
Owner

@StefH commented on GitHub (Oct 1, 2020):

@cezarypiatekGC What do you think of my previous answer?

@StefH commented on GitHub (Oct 1, 2020): @cezarypiatekGC What do you think of my previous answer?
Author
Owner

@cezarypiatekGC commented on GitHub (Oct 6, 2020):

I started using WithMapping method to define mocks (and created my own abstraction over this tedious API), but there are some discrepancies in the final behavior of those two API's, the problem is reported here https://github.com/WireMock-Net/WireMock.Net/issues/504

@cezarypiatekGC commented on GitHub (Oct 6, 2020): I started using `WithMapping` method to define mocks (and created my own abstraction over this tedious API), but there are some discrepancies in the final behavior of those two API's, the problem is reported here https://github.com/WireMock-Net/WireMock.Net/issues/504
Author
Owner

@StefH commented on GitHub (Oct 6, 2020):

Hello @cezarypiatekGC,

If you want to test a preview fix for this issue, in order to test that you need MyGet version WireMock.Net.1.2.18-ci-13754.nupkg.

Can you test if that one does work correct?

@StefH commented on GitHub (Oct 6, 2020): Hello @cezarypiatekGC, If you want to test a preview fix for this issue, in order to test that you need MyGet version `WireMock.Net.1.2.18-ci-13754.nupkg`. Can you test if that one does work correct?
Author
Owner

@StefH commented on GitHub (Mar 3, 2022):

@cezarypiatekGC
I close this issue.

In case you still have questions, just create a new issue.

@StefH commented on GitHub (Mar 3, 2022): @cezarypiatekGC I close this issue. In case you still have questions, just create a new issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#293