In this article, let's learn about how to use
Functional Test in
WebAPI in ASP.NET Core.
Note: If you have not done so already, I recommend you read the article on Faking Dependencies in Functional testing in ASP.NET WEB API.
Table of Contents
In our previous article we saw how to replace services in functional test and that approach has a drawback of writing lot of code to test all the
possible scenarios. We can overcome this drawback by using
WireMock.NET. It helps us to simplify the test code
and also helps us to test all the possible scenarios. In this article, we'll learn about how to use
in functional test.
Let's start by adding
Task<WeatherForecast> GetWeatherForecast() to our
IExternalAPIService and implement it in
ExternalAPIService. Before we dive
into this implementation, lets first add URL and
HttpClient configuration in our
Code Sample - App Settings for External API Call in Web API
Now in the
Program.cs, lets add the
HttpClient configuration as shown below.
Code Sample - HTTP Client Configuration
Now all we need to do is to inject the above
WeatherClient HTTP Client inside our
ExternalAPIService and make a call to get weather information as shown below.
Code Sample - External API call to get weather information
Next we need to call the
GetWeatherForecast method from our
as shown below.
Here is the swagger response from the
Why to use WireMock.NET ?
WireMock.NET is a powerful tool that can accurately emulate the behavior of an
capturing incoming requests and directing them to a WireMock.NET HTTP server. This capability grants us the
ability to define expectations, call the API, and verify its behavior, making it ideal for extensive testing of code interactions with external services.
we can effectively mock real API endpoints and utilize its comprehensive features for HTTP response stubbing,
including matching stubs based on URL/Path, headers, cookies, and body content patterns.
The benefits of using WireMock.NET in functional tests include:
No need to write lot of mock code for HTTP calls. WireMock.NET helps to test actual behavior.
- WireMock.NET helps when
External API calls have rate limitation.
Easy to setupand cover all possible status code behaviors.
The steps to implement WireMock.NET in functional test are as follows:
Add reference to
WireMock.NETNuget Package in
FunctionalTestproject and right click on the file and select
Copy to Output Directory.
Now configure the
FunctionalTest.csas shown below.
Code Sample - appsettings.Test.json configuration in Functional Test
The above configuration will replace the Weather API URL in the test. Now we need to setup the WireMock.NET server. For that we need to create
Fixtureto be used in all the tests. So lets create a
ExternalAPIFixture classas shown below
Code Sample - WireMock ExternalAPI Fixture to respond to API Calls
classis responsible for starting the
https modeand also for stopping it. The key thing is the
SetupGetWeatherwhere we setup request matching particular Path in WireMock.NET server and also the response to be returned. With thi setup when we call External API in Functional Test WireMock kicks in and when the path matches, it will give the expected response to be
You can refer to the
WireMock.NETdocumentation for more details on how to setup the request and response for various cases like timeouts, auth failure, etc.
Now we are left with our test. So lets add the test as shown below.
Code Sample - Get From API Functional Test using WireMock.NET
In the above code if you notice, we are injecting
ExternalAPIFixtureand using it to setup the WireMock.NET before running the test and also to stop the WireMock.NET server after the test. The
SetupGetWeathermethod is responsible for setting up the WireMock.NET server to respond to the request.
Thats it. We are done with setup. Now the tests will run in the following flow.
- Start Test Method.
- Create HTTP Client.
- Create WebApplicationFactory.
- Run Program.cs.
- Service Registration.
- Overriding Service Registration and Configuration with ConfigureTestServices.
- Build Web Application.
- WireMock.NET Setup.
- Test Code Execution.
Now lets run the test and see the result.
In this article, we learnt about how to use
WireMock.NET in functional test in ASP.NET Web API. We also saw the
benefits of using
WireMock.NET in functional test. The complete source code for this article can be found
here. Hope you find this information useful.
In our next article we will continue to learn how to setup
Authentication in Functional Test in ASP.NET Web API.