Using Wiremock Net Testcontainers
WireMock.Net.Testcontainers
Section titled “WireMock.Net.Testcontainers”WireMock.Net.Testcontainers uses Testcontainers for .NET to spinup a docker container directly from the C# (unittest) code.
This options requires docker service running locally.
Both the Linux and the Windows version from WireMock.Net are supported 📝.
📝 It’s not needed to specify the version, this is determined automatically. (So if you are running Docker on a Windows Host, the Windows Docker image is used, else the Linux Docker image is used.
Build and Start
Section titled “Build and Start”To build a container and startup this container, use this code:
var container = new WireMockContainerBuilder() .WithAutoRemove(true) .WithCleanUp(true) .Build();
await container.StartAsync().ConfigureAwait(false);
Methods
Section titled “Methods”The following builder methods are available for the WireMockContainerBuilder
:
Method | Example | What |
---|---|---|
WithMappings | .WithMappings(@"C:\example\\mappings") | Specifies the path for the (static) mapping json files. |
WithWatchStaticMappings | .WithWatchStaticMappings(true) | Watch the static mapping files + folder for changes when running. |
WithAdminUserNameAndPassword | .WithAdminUserNameAndPassword("x", "y") | Set the admin username. and password for the container (basic authentication). |
WithImage | .WithImage("sheyenrath/wiremock.net-alpine:1.6.4") | You can provide a specific image + tag. |
Create a Admin Client
Section titled “Create a Admin Client”Use the following code to get a RestEase Admin Client for this running container instance.
var restEaseApiClient = container.CreateWireMockAdminClient();
Create a HTTP Client
Section titled “Create a HTTP Client”Use the following code to get a HTTP Client for this running container instance to call WireMock.Net
var client = container.CreateClient();var result = await client.GetStringAsync("/test123");
Usage in Unit Test
Section titled “Usage in Unit Test”Follow the tutorial here and make sure to use WireMock.Net container instead of the container used in that example.