Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WireMock not working in Debug within test project #510

Closed
xadvfh opened this issue Sep 30, 2020 · 23 comments
Closed

WireMock not working in Debug within test project #510

xadvfh opened this issue Sep 30, 2020 · 23 comments
Labels

Comments

@xadvfh
Copy link

xadvfh commented 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.

@xadvfh xadvfh added the bug label Sep 30, 2020
@StefH
Copy link
Collaborator

StefH commented Sep 30, 2020

Please provide a full working example.

@xadvfh
Copy link
Author

xadvfh commented 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);
        }
    }
}

@StefH
Copy link
Collaborator

StefH commented 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.

@xadvfh
Copy link
Author

xadvfh commented 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.

@StefH
Copy link
Collaborator

StefH commented Oct 1, 2020

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

@xadvfh
Copy link
Author

xadvfh commented Oct 1, 2020

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

@StefH
Copy link
Collaborator

StefH commented 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.

@xadvfh
Copy link
Author

xadvfh commented 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);
        }
    }
}

@StefH
Copy link
Collaborator

StefH commented 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

@xadvfh
Copy link
Author

xadvfh commented Oct 1, 2020

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

@StefH
Copy link
Collaborator

StefH commented 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

@xadvfh
Copy link
Author

xadvfh commented 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?

@StefH
Copy link
Collaborator

StefH commented 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?

@xadvfh
Copy link
Author

xadvfh commented 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.

@StefH
Copy link
Collaborator

StefH commented 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));

@xadvfh
Copy link
Author

xadvfh commented 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
Copy link
Author

xadvfh commented 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?

@StefH
Copy link
Collaborator

StefH commented 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
Copy link
Collaborator

StefH commented Feb 4, 2021

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

@xadvfh
Copy link
Author

xadvfh commented Feb 4, 2021

@StefH I still have this issue.

@xadvfh
Copy link
Author

xadvfh commented 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.

@StefH
Copy link
Collaborator

StefH commented Feb 20, 2022

Hello @xadvfh,

Did you try the latest version yet?

@StefH
Copy link
Collaborator

StefH commented Feb 27, 2022

@xadvfh : can you please check the latest version ?

@StefH StefH closed this as completed Jun 9, 2022
@StefH StefH added question and removed bug labels Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants