A demonstration project showing how to use WireMock.NET for mocking external API dependencies in a .NET application.
This entire project was generated from scratch using:
- Cursor IDE (version 0.46.11)
- Claude 3.7 Sonnet LLM
- Windows 11 PC
The initial project generation took approximately 10 minutes, with an additional 30 minutes for tweaks and refinements (mainly to this readme file!). This demonstrates the power of AI-assisted development for rapidly creating functional demo applications.
The idea for this project was inspired by an amazing coder and good friend, Adrian Cope.
This project primarily demonstrates the power of AI-assisted development using Cursor IDE and Claude 3.7 Sonnet. It showcases how quickly a functional application can be created with AI assistance.
Technically, the project demonstrates how to use WireMock.NET to create mock HTTP responses for testing and development purposes. It includes:
- A .NET API with controllers that return JSON data in different formats (camelCase and PascalCase)
- A WireMock server that simulates an external API
- Unit tests that use a mock service implementation
-
src/WiremockDemo.Api: The main API project
- Controllers: Contains the API controllers
WeatherController.cs
: Handles HTTP requests and returns weather data
- Models: Contains the data models
WeatherData.cs
: Defines the weather data structure
- Services: Contains the service layer that calls the external API
ExternalService.cs
: Implements the IExternalService interface to call the external API
- Wiremock: Contains the WireMock server implementation
WiremockServer.cs
: Sets up and configures the WireMock server
- Controllers: Contains the API controllers
-
tests/WiremockDemo.Tests: The test project
WeatherControllerTests.cs
: Contains unit tests for the API controllersMockExternalService.cs
: Includes a mock implementation of the external serviceCustomWebApplicationFactory.cs
: Configures the test environment
- .NET 8.0 SDK or later
- Cursor IDE (or another AI-powered IDE for the full experience, as this project demonstrates AI-assisted development)
- Clone the repository
- Navigate to the project directory
- Run the API:
cd src/WiremockDemo.Api
dotnet run
- Access the API endpoints:
http://localhost:5049/api/weatherCamelCase
- Returns JSON with camelCase propertieshttp://localhost:5049/api/weatherPascalCase
- Returns JSON with PascalCase properties
cd tests/WiremockDemo.Tests
dotnet test
Or from the root directory:
dotnet test
The WireMock server is implemented in WiremockServer.cs
and is responsible for:
- Starting a WireMock.NET server on port 9090
- Setting up mock responses for different API endpoints
- Configuring JSON serialization options for different response formats
The server is registered as a singleton in the dependency injection container and is automatically started when the application runs in development mode.
The ExternalService
class is responsible for:
- Making HTTP requests to the WireMock server
- Deserializing the responses using the appropriate JSON options
- Returning the data to the controllers
In a real-world scenario, this service would call an actual external API, but for demonstration purposes, it calls the WireMock server running locally.
The WeatherController
class exposes two endpoints:
GET /api/weatherCamelCase
- Returns weather data with camelCase property namesGET /api/weatherPascalCase
- Returns weather data with PascalCase property names
The test project includes:
- Unit tests for the
WeatherController
class - A mock implementation of the
IExternalService
interface - A custom
WebApplicationFactory
for integration testing
The tests verify that the controllers return the expected data and handle errors correctly.
This project is licensed under the MIT License - see the LICENSE file for details.