WireMock.Net icon indicating copy to clipboard operation
WireMock.Net copied to clipboard

Unable to build WireMockContainerBuilder with custom image

Open OliBomby opened this issue 1 month ago • 3 comments

Describe the bug

Custom WireMock images that include their own mappings like from the guide can not be used in WireMockContainerBuilder.WithImage() leading to a InvalidOperationException on Build() because _imageOS is never set.

Expected behavior:

var container = new WireMockContainerBuilder()
            .WithLinuxImage()
            .WithImage("custom-wiremock:latest")
            .WithAutoRemove(true)
            .WithCleanUp(true)
            .Build();

await container.StartAsync().ConfigureAwait(false);
var client = container.CreateClient();
// Start requesting the built-in mappings

I expect with the above code to work an not throw an exception. However after WithLinuxImage() _imageOS is still set to null because the builder objects gets recreated from scratch with only the docker config remaining.

OliBomby avatar Dec 09 '25 14:12 OliBomby

@OliBomby Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version). This will not work.

StefH avatar Dec 09 '25 14:12 StefH

Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version). This will not work.

Ah I did not realize there was a distinction in the WireMock version used in this Docker image.

I still think it should work though. It almost works just like this, if it didn't throw the InvalidOperationException the rest of my code would've worked. The REST API of the Java and .NET versions of WireMock are almost the same.

Here's a minimal working example of using the Java image in a .NET test container:

var container = new ContainerBuilder()
    .WithImage("custom-wiremock:latest")
    .WithPortBinding(8080, true)
    .WithAutoRemove(true)
    .WithCleanUp(true)
    .Build();

await container.StartAsync().ConfigureAwait(false);

// Wait for WireMock to start
Thread.Sleep(5000);

var client = new HttpClient();
client.BaseAddress = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPorts()[8080]).Uri;

This code would be much cleaner if I could make use of the WireMockContainerBuilder and WireMockContainer classes, so that would be really nice to have.

OliBomby avatar Dec 09 '25 15:12 OliBomby

@OliBomby I understand your question. Currently, it's indeed not possible to provide a custom WireMock.Net or wiremock-org (java) image. I can try to add that logic.

However, the mappings are not 100% the same, so using the container.CreateClient(); will not work correct for you. The best would be that I create a new NuGet named WireMock.Org.Testcontainers which will use a default java docker image and also provide code to provide your own custom image.

StefH avatar Dec 10 '25 07:12 StefH

I did fix it for WireMock.Net.Testcontainers --> https://github.com/wiremock/WireMock.Net/pull/1391

StefH avatar Dec 11 '25 08:12 StefH

The WireMock.Net.Testcontainers is updated to accept a custom image and set the _imageOS. I will publish a new NuGet soon.


However, I cannot give any guarantee that this works for the wiremock java version.

StefH avatar Dec 11 '25 10:12 StefH

I'm afraid this implementation still won't work for any image which doesn't have wiremock.net in the name, because _imageOS will still be null in the builder object returned by WithCustomImage, because the base.WithImage method creates a new builder object and doesn't retain the value for _imagesOS. https://github.com/testcontainers/testcontainers-dotnet/blob/3f9de427b877a4fee394eab354fd98abbee06417/src/Testcontainers/Builders/ContainerBuilder%603.cs#L93-L96 https://github.com/wiremock/WireMock.Net/blob/e3e2dc7c6506e4428d55d1154e247de751c8c6cb/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs#L281-L284

OliBomby avatar Dec 11 '25 12:12 OliBomby

I see

StefH avatar Dec 11 '25 13:12 StefH

https://github.com/wiremock/WireMock.Net/pull/1392

StefH avatar Dec 11 '25 21:12 StefH