What are the modifications needed to be done to get Wiremock running?

220 Views Asked by At

I have a .Net Core web API solution called ReportService, which calls another API endpoint (we can call this PayrollService) to get payroll reports. So my requirement is to mock the PayrollService using Wiremock.Net.

Also currently I have a automation test case written, which will directly call the ReportService controller and will execute all the service logic, and also classes which calls PayrollService and the DB layer logic and will get the HTTP result back from the ReportService.

Please note that the Automation test cases is a separate solution. So my requirement is to run the automation test cases like before on ReportService, and the payroll service will be mocked by Wiremock.

So, what are the changes that need to happen in the codebase? Do we have to change the url of the ReportService to be the Wiremock server base url in the ReportService solution? Please let us know, and please use the terms I have used in the question regarding the project names so I am clear.

1

There are 1 best solutions below

0
On

Your assumption is indeed correct, you have make the base URL which is used by ReportService configurable.

So that for your unit / integration tests you can provide the URL on which the WireMock.Net server is running.

Example:

[Test]
public async Task ReportService_Should_Call_External_API_And_Get_Report()
{
  // Arrange (start WireMock.Net server)
  var server = WireMockServer.Start();
  
  // Setup your mapping
  server
    .Given(Request.Create().WithPath("/foo").UsingGet())
    .RespondWith(
      Response.Create()
        .WithStatusCode(200)
        .WithBody(@"{ ""msg"": ""Hello world!"" }")
    );

  // Act (configure your ReportService to connect to the URL where WireMock.Net is running)
  var reportService = new ReportService(server.Urls[0]});
  
  var response = reportService.GetResport();
    
  // Assert
  Assert.Equal(response, ...); // Verify
}