Wiremock server fails to respond in framework 4.8 NUnit V3 test project class library #274

Closed
opened 2025-12-29 15:19:31 +01:00 by adam · 3 comments
Owner

Originally created by @EthanM11122 on GitHub (May 21, 2020).

I'm trying to use wiremock to stub an endpoint in unit tests and I'm struggling to identify why it isn't working. I'm using Nunit in a .net framework 4.8 class library. When running the test normally it sits for 1 minute and then fails as the request's response status equals 0 not the expected 200.
If debugging the test it fails much faster for the same reason. I don't see any errors thrown but the response object has an error message
"The underlying connection was closed: An unexpected error occurred on a receive."
it appears that the server can't respond I suspect due to a deadlock.

The sample test I have is below. I feel I must be making a simple mistake somewhere as when I convert the test class library to a console application the test passes. Does an obvious mistake stand out to anyone?

image

    using WireMock.RequestBuilders;
    using WireMock.ResponseBuilders;
    using WireMock.Server;
    using NUnit.Framework;
    using RestSharp;
    namespace UnitTest
    {
        /*  class Program
        {
            static void Main(string[] args)
            {
            }
        }*/
        [TestFixture()]
        class Test
        {
            private WireMockServer _server;
            [SetUp()]
            public void Setup()
            {
                _server = WireMockServer.Start();
                _server
                  .Given(
                    Request.Create().WithPath("/some/thing").UsingGet()
                  )
                  .RespondWith(
                    Response.Create()
                      .WithStatusCode(200)
                      .WithHeader("Content-Type", "text/plain")
                      .WithBody("Hello world!")
                  );
            }
            [Test()]
            public void Test1()
            {
                var client = new RestClient(_server.Urls[0]);
                var request = new RestRequest("/some/thing");
                var response = client.Execute(request);
                Assert.AreEqual(200, (int)response.StatusCode);
            }
        }
    }

Originally created by @EthanM11122 on GitHub (May 21, 2020). I'm trying to use wiremock to stub an endpoint in unit tests and I'm struggling to identify why it isn't working. I'm using Nunit in a .net framework 4.8 class library. When running the test normally it sits for 1 minute and then fails as the request's response status equals 0 not the expected 200. If debugging the test it fails much faster for the same reason. I don't see any errors thrown but the response object has an error message "The underlying connection was closed: An unexpected error occurred on a receive." it appears that the server can't respond I suspect due to a deadlock. The sample test I have is below. I feel I must be making a simple mistake somewhere as when I convert the test class library to a console application the test passes. Does an obvious mistake stand out to anyone? ![image](https://user-images.githubusercontent.com/50234028/82548899-fe581e00-9b53-11ea-8bd2-a53e80f19daf.png) ``` cs using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; using NUnit.Framework; using RestSharp; namespace UnitTest { /* class Program { static void Main(string[] args) { } }*/ [TestFixture()] class Test { private WireMockServer _server; [SetUp()] public void Setup() { _server = WireMockServer.Start(); _server .Given( Request.Create().WithPath("/some/thing").UsingGet() ) .RespondWith( Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "text/plain") .WithBody("Hello world!") ); } [Test()] public void Test1() { var client = new RestClient(_server.Urls[0]); var request = new RestRequest("/some/thing"); var response = client.Execute(request); Assert.AreEqual(200, (int)response.StatusCode); } } } ```
adam added the question label 2025-12-29 15:19:31 +01:00
adam closed this issue 2025-12-29 15:19:31 +01:00
Author
Owner

@StefH commented on GitHub (May 21, 2020):

No Sorry, I cannot see any problems.

I did create a new NUnit project with this csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="nunit" Version="3.12.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
    <PackageReference Include="RestSharp" Version="106.11.4" />
    <PackageReference Include="WireMock.Net" Version="1.2.11" />
  </ItemGroup>

</Project>

And the test class looks like:

using NUnit.Framework;
using RestSharp;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace NUnitTestProject
{
    [TestFixture]
    public class Tests
    {
        private WireMockServer _server;

        [SetUp]
        public void Setup()
        {
            _server = WireMockServer.Start();
            _server
                .Given(
                    Request.Create().WithPath("/some/thing").UsingGet()
                )
                .RespondWith(
                    Response.Create()
                        .WithStatusCode(200)
                        .WithHeader("Content-Type", "text/plain")
                        .WithBody("Hello world!")
                );
        }

        [Test]
        public void Test1()
        {
            var client = new RestClient(_server.Urls[0]);
            var request = new RestRequest("/some/thing");
            var response = client.Execute(request);
            Assert.AreEqual(200, (int)response.StatusCode);
        }
    }
}
@StefH commented on GitHub (May 21, 2020): No Sorry, I cannot see any problems. I did create a new NUnit project with this csproj: ``` xml <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net472</TargetFramework> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="nunit" Version="3.12.0" /> <PackageReference Include="NUnit3TestAdapter" Version="3.15.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> <PackageReference Include="RestSharp" Version="106.11.4" /> <PackageReference Include="WireMock.Net" Version="1.2.11" /> </ItemGroup> </Project> ``` And the test class looks like: ``` cs using NUnit.Framework; using RestSharp; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; namespace NUnitTestProject { [TestFixture] public class Tests { private WireMockServer _server; [SetUp] public void Setup() { _server = WireMockServer.Start(); _server .Given( Request.Create().WithPath("/some/thing").UsingGet() ) .RespondWith( Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "text/plain") .WithBody("Hello world!") ); } [Test] public void Test1() { var client = new RestClient(_server.Urls[0]); var request = new RestRequest("/some/thing"); var response = client.Execute(request); Assert.AreEqual(200, (int)response.StatusCode); } } } ```
Author
Owner

@EthanM11122 commented on GitHub (May 21, 2020):

Thanks @StefH, I was using a toolset generated project file when i moved to the SDK generated format it is now working.

@EthanM11122 commented on GitHub (May 21, 2020): Thanks @StefH, I was using a toolset generated project file when i moved to the SDK generated format it is now working.
Author
Owner

@StefH commented on GitHub (May 21, 2020):

Ok

@StefH commented on GitHub (May 21, 2020): Ok
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#274