WireMock not working in Debug within test project #297

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

Originally created by @xadvfh on GitHub (Sep 30, 2020).

Describe the bug

When you place a breakpoint and try to make a call to the mock server via an external tool (browser), there is no response. This was first seen in #122

It would be nice if this worked since you could easily check if the json response bodies you're creating are working as expected. Right now I have to add a long Thread.Sleep() to check my work.

Expected behavior:

Server should be listening in debug mode.

Test to reproduce

  • Create nunit project and set up wiremock as normal
  • Place a breakpoint somewhere after stubbing a request
  • Call endpoint from a browser/postman/etc.

I'm using dotnet core 3.1 and nunit 3.

Originally created by @xadvfh on GitHub (Sep 30, 2020). ### Describe the bug When you place a breakpoint and try to make a call to the mock server via an external tool (browser), there is no response. This was first seen in #122 It would be nice if this worked since you could easily check if the json response bodies you're creating are working as expected. Right now I have to add a long `Thread.Sleep()` to check my work. ### Expected behavior: Server should be listening in debug mode. ### Test to reproduce - Create nunit project and set up wiremock as normal - Place a breakpoint somewhere after stubbing a request - Call endpoint from a browser/postman/etc. ### Other related info I'm using dotnet core 3.1 and nunit 3.
adam added the question label 2025-12-29 08:25:40 +01:00
adam closed this issue 2025-12-29 08:25:40 +01:00
Author
Owner

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

Please provide a full working example.

@StefH commented on GitHub (Sep 30, 2020): Please provide a full working example.
Author
Owner

@xadvfh commented on GitHub (Sep 30, 2020):

Thank you for the fast response, here is a full working example:

using NUnit.Framework;
using System.Threading;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace NUnitTestProject1
{
    public class Tests
    {
        WireMockServer server;

        [SetUp]
        public void Setup()
        {
            //choosing a port so we know which port to hit when testing this.
            server = WireMockServer.Start(port: 50100);
        }


        [Test]
        public void Test_With_Breakpoint()
        {
            server.Given(
                    Request.Create().WithPath("/test"))
                .RespondWith(Response.Create().WithBody("Breakpoint Test"));
        } // Breakpoint here


        [Test]
        public void Test_Without_Breakpoint()
        {
            server.Given(
                    Request.Create().WithPath("/test"))
                .RespondWith(Response.Create().WithBody("Sleep Test"));

            Thread.Sleep(60000);
        }
    }
}
@xadvfh commented on GitHub (Sep 30, 2020): Thank you for the fast response, here is a full working example: ```C# using NUnit.Framework; using System.Threading; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; namespace NUnitTestProject1 { public class Tests { WireMockServer server; [SetUp] public void Setup() { //choosing a port so we know which port to hit when testing this. server = WireMockServer.Start(port: 50100); } [Test] public void Test_With_Breakpoint() { server.Given( Request.Create().WithPath("/test")) .RespondWith(Response.Create().WithBody("Breakpoint Test")); } // Breakpoint here [Test] public void Test_Without_Breakpoint() { server.Given( Request.Create().WithPath("/test")) .RespondWith(Response.Create().WithBody("Sleep Test")); Thread.Sleep(60000); } } } ```
Author
Owner

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

@xadvfh
Some things which could maybe help you:

  • Use [OneTimeSetUp] instead of [SetUp]
  • Maybe switch to xunit?

My own development environment has some issues now, so I cannot reproduce your issue right now. I'll come back to you.

@StefH commented on GitHub (Sep 30, 2020): @xadvfh Some things which could maybe help you: - Use `[OneTimeSetUp]` instead of `[SetUp]` - Maybe switch to xunit? My own development environment has some issues now, so I cannot reproduce your issue right now. I'll come back to you.
Author
Owner

@xadvfh commented on GitHub (Sep 30, 2020):

[OneTimeSetup] is only executed once before ALL tests are run. If we want a new server instance for each test we would have to use [Setup].

I'll try with xunit later today and see if that helps.

@xadvfh commented on GitHub (Sep 30, 2020): `[OneTimeSetup]` is only executed once before ALL tests are run. If we want a new server instance for each test we would have to use `[Setup]`. I'll try with xunit later today and see if that helps.
Author
Owner

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

@xadvfh In the [Setup] you can use the server.Reset() method to reset all mappings before each test.

@StefH commented on GitHub (Oct 1, 2020): @xadvfh In the `[Setup]` you can use the `server.Reset()` method to reset all mappings before each test.
Author
Owner

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

@StefH how would that work in running tests in parallel. Resetting would delete all mappings for other tests currently running, no?

@xadvfh commented on GitHub (Oct 1, 2020): @StefH how would that work in running tests in parallel. Resetting would delete all mappings for other tests currently running, no?
Author
Owner

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

I'm not 100% sure for NUnit, but I think that all tests within a single .cs file are run sequentially.
At least I'm 100% sure that XUnit does it like this.

@StefH commented on GitHub (Oct 1, 2020): I'm not 100% sure for NUnit, but I think that all tests within a single .cs file are run sequentially. At least I'm 100% sure that XUnit does it like this.
Author
Owner

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

So I just tested with the latest version of xunit and it has the same problem. I even removed any setup related stuff and am creating the server in the test itself. Working example:

using System.Threading;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using Xunit;

namespace XUnitTestProject
{
    public class Tests
    {
        [Fact]
        public void Test_With_Breakpoint()
        {
            var server =  WireMockServer.Start(port: 50100);

            server.Given(
                    Request.Create().WithPath("/test"))
                .RespondWith(Response.Create().WithBody("Breakpoint Test"));
        } // Breakpoint here


        [Fact]
        public void Test_Without_Breakpoint()
        {
            var server = WireMockServer.Start(port: 50100);

            server.Given(
                    Request.Create().WithPath("/test"))
                .RespondWith(Response.Create().WithBody("Sleep Test"));

            Thread.Sleep(60000);
        }
    }
}
@xadvfh commented on GitHub (Oct 1, 2020): So I just tested with the latest version of xunit and it has the same problem. I even removed any setup related stuff and am creating the server in the test itself. Working example: ```c# using System.Threading; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; using Xunit; namespace XUnitTestProject { public class Tests { [Fact] public void Test_With_Breakpoint() { var server = WireMockServer.Start(port: 50100); server.Given( Request.Create().WithPath("/test")) .RespondWith(Response.Create().WithBody("Breakpoint Test")); } // Breakpoint here [Fact] public void Test_Without_Breakpoint() { var server = WireMockServer.Start(port: 50100); server.Given( Request.Create().WithPath("/test")) .RespondWith(Response.Create().WithBody("Sleep Test")); Thread.Sleep(60000); } } } ```
Author
Owner

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

My NUnit test class:

using System.Threading;
using NUnit.Framework;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace NUnitTestProject1
{
    public class Tests
    {
        WireMockServer server;

        [OneTimeSetUp]
        public void Setup()
        {
            server = WireMockServer.Start();
        }

        [Test]
        public void Test_With_Breakpoint()
        {
            int x = 0;
            server.Reset();
            server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test"));
            int y = 0;
        } // Breakpoint here


        [Test]
        public void Test_Without_Breakpoint()
        {
            int x = 0;
            server.Reset();
            server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Sleep Test"));

            Thread.Sleep(1000);
            int y = 0;
        }
    }
}

However when debugging, I get this message to allow access:
image

And debugging via menu:
image

Works fine:
image

@StefH commented on GitHub (Oct 1, 2020): My NUnit test class: ```cs using System.Threading; using NUnit.Framework; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; namespace NUnitTestProject1 { public class Tests { WireMockServer server; [OneTimeSetUp] public void Setup() { server = WireMockServer.Start(); } [Test] public void Test_With_Breakpoint() { int x = 0; server.Reset(); server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test")); int y = 0; } // Breakpoint here [Test] public void Test_Without_Breakpoint() { int x = 0; server.Reset(); server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Sleep Test")); Thread.Sleep(1000); int y = 0; } } } ``` However when debugging, I get this message to allow access: ![image](https://user-images.githubusercontent.com/249938/94819758-eba5fd80-03ff-11eb-904c-c4232bf711ae.png) And debugging via menu: ![image](https://user-images.githubusercontent.com/249938/94819867-11330700-0400-11eb-877a-23cff91ea212.png) Works fine: ![image](https://user-images.githubusercontent.com/249938/94819819-ffe9fa80-03ff-11eb-8ba3-98c0e117bfb0.png)
Author
Owner

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

Were you able to make a request from a browser/postman while stopped at a breakpoint after the stubbing(line 24)?

@xadvfh commented on GitHub (Oct 1, 2020): Were you able to make a request from a browser/postman while stopped at a breakpoint after the stubbing(line 24)?
Author
Owner

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

No, this is not possible. UnitTesting with WireMock needs to be done completely in the unit-test like this:

[Test]
public async Task Test_With_Breakpoint()
{
    int x = 0;
    server.Reset();
    server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test"));

    var client = new HttpClient();
    var response = await client.GetAsync(server.Urls.First() + "/test");

    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

    var content = await response.Content.ReadAsStringAsync();
    Assert.AreEqual("Breakpoint Test", content);
    int y = 0;
} // Breakpoint here

Screenshot:
image

@StefH commented on GitHub (Oct 1, 2020): No, this is not possible. UnitTesting with WireMock needs to be done completely in the unit-test like this: ``` c# [Test] public async Task Test_With_Breakpoint() { int x = 0; server.Reset(); server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test")); var client = new HttpClient(); var response = await client.GetAsync(server.Urls.First() + "/test"); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var content = await response.Content.ReadAsStringAsync(); Assert.AreEqual("Breakpoint Test", content); int y = 0; } // Breakpoint here ``` Screenshot: ![image](https://user-images.githubusercontent.com/249938/94821503-ee095700-0401-11eb-9988-78f6eb50e3c8.png)
Author
Owner

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

I understand. My usecase is that we're creating complex json object and during the creation phase it would be nice to be able to see what we're creating in a more readable manner. Is there a technical limitation why setting the breakpoint doesn't work but adding a long sleep works fine?

@xadvfh commented on GitHub (Oct 1, 2020): I understand. My usecase is that we're creating complex json object and during the creation phase it would be nice to be able to see what we're creating in a more readable manner. Is there a technical limitation why setting the breakpoint doesn't work but adding a long sleep works fine?
Author
Owner

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

The debugger probably stops several threads, so also the code which listens and responses to requests.

About complex json, is this the response message?

@StefH commented on GitHub (Oct 1, 2020): The debugger probably stops several threads, so also the code which listens and responses to requests. About complex json, is this the response message?
Author
Owner

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

The debugger probably stops several threads, so also the code which listens and responses to requests.

Is there a way you recommend setting up the wiremock server so we can do debugging? for example, if you create an asp.net core API you're able to debug.

About complex json, is this the reponse message?

Yes. we're mocking an API that is used by a UI. We have models set up but there are nested objects. On top of that, we have created builders to allow test writers to create responses dynamically based on what they need.

@xadvfh commented on GitHub (Oct 1, 2020): > The debugger probably stops several threads, so also the code which listens and responses to requests. Is there a way you recommend setting up the wiremock server so we can do debugging? for example, if you create an asp.net core API you're able to debug. > About complex json, is this the reponse message? Yes. we're mocking an API that is used by a UI. We have models set up but there are nested objects. On top of that, we have created builders to allow test writers to create responses dynamically based on what they need.
Author
Owner

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

If you just want to verify if the JSON is correct, then the easiest solution is to created your complex nested objects and serialize these to a json-stirng yourself.

And then provide this json-string to the code:
server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody(json_string));

@StefH commented on GitHub (Oct 2, 2020): If you just want to verify if the JSON is correct, then the easiest solution is to created your complex nested objects and serialize these to a json-stirng yourself. And then provide this json-string to the code: `server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody(json_string));`
Author
Owner

@xadvfh commented on GitHub (Oct 2, 2020):

do you have an idea of where in the codebase this issue could be happening? maybe I can do some digging and see if we can fix this.

@xadvfh commented on GitHub (Oct 2, 2020): do you have an idea of where in the codebase this issue could be happening? maybe I can do some digging and see if we can fix this.
Author
Owner

@xadvfh commented on GitHub (Nov 2, 2020):

@StefH just checking in to see if there is anything I can help with to fix this. Is there a place in the code you would recommend to start troubleshooting?

@xadvfh commented on GitHub (Nov 2, 2020): @StefH just checking in to see if there is anything I can help with to fix this. Is there a place in the code you would recommend to start troubleshooting?
Author
Owner

@StefH commented on GitHub (Nov 2, 2020):

Maybe change the async behavior from the GetAsync?

[Test]
public async Task Test_With_Breakpoint()
{
    int x = 0;
    server.Reset();
    server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test"));

    var client = new HttpClient();
    var responseTask = client.GetAsync(server.Urls.First() + "/test");

    // put breakpoint here + debug?

    

    var content = await (await responseTask).Content.ReadAsStringAsync();
    Assert.AreEqual("Breakpoint Test", content);
} // Breakpoint here
@StefH commented on GitHub (Nov 2, 2020): Maybe change the async behavior from the GetAsync? ```c# [Test] public async Task Test_With_Breakpoint() { int x = 0; server.Reset(); server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test")); var client = new HttpClient(); var responseTask = client.GetAsync(server.Urls.First() + "/test"); // put breakpoint here + debug? var content = await (await responseTask).Content.ReadAsStringAsync(); Assert.AreEqual("Breakpoint Test", content); } // Breakpoint here ```
Author
Owner

@StefH commented on GitHub (Feb 4, 2021):

@xadvfh Do you still have an issue? Or can this one be closed?

@StefH commented on GitHub (Feb 4, 2021): @xadvfh Do you still have an issue? Or can this one be closed?
Author
Owner

@xadvfh commented on GitHub (Feb 4, 2021):

@StefH I still have this issue.

@xadvfh commented on GitHub (Feb 4, 2021): @StefH I still have this issue.
Author
Owner

@xadvfh commented on GitHub (Feb 10, 2021):

@StefH I tried this again yesterday to wanted to confirm this issue still exists.

My use case is that I am using WireMock.Net to replace API calls that are made by our website. At times i want to place a breakpoint to pause the test and click around in the website. However, anything that triggers a call to wiremock will not respond while i am in a breakpoint.

@xadvfh commented on GitHub (Feb 10, 2021): @StefH I tried this again yesterday to wanted to confirm this issue still exists. My use case is that I am using WireMock.Net to replace API calls that are made by our website. At times i want to place a breakpoint to pause the test and click around in the website. However, anything that triggers a call to wiremock will not respond while i am in a breakpoint.
Author
Owner

@StefH commented on GitHub (Feb 20, 2022):

Hello @xadvfh,

Did you try the latest version yet?

@StefH commented on GitHub (Feb 20, 2022): Hello @xadvfh, Did you try the latest version yet?
Author
Owner

@StefH commented on GitHub (Feb 27, 2022):

@xadvfh : can you please check the latest version ?

@StefH commented on GitHub (Feb 27, 2022): @xadvfh : can you please check the latest version ?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#297