Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Test your .NET exceptions with FluentAssertions

Test your .NET exceptions with FluentAssertions
December 21, 2022 2 min read BEN ABT
.NET

This article was written in 2022. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.

Testing exceptions in your code is an important part of ensuring that your code is robust and behaves correctly under various scenarios. One way to test exceptions in .NET is by using the FluentAssertions lib.

Prerequisites

Before we get started, you’ll need to install the FluentAssertions library. You can do this via NuGet by running the following command:

Shell
1Install-Package FluentAssertions
2# or
3dotnet add package FluentAssertions

To use FluentAssetions you have to add the following using statement to your code:

C#
using FluentAssertions;

Testing for Exceptions

The most basic way to test for exceptions with FluentAssertions is to use the Should().Throw() method. This method allows you to specify the type of exception that you expect to be thrown and it will fail the test if the exception is not thrown or if a different exception is thrown.

Here’s an example of how to use Should().Throw() to test for exceptions:

C#
 1
 2[Test] // or [Fact] if you're using XUnit
 3public void TestMethod_ShouldThrowException()
 4{
 5    // arrange
 6    SomeObject someObject = new();
 7
 8    // act
 9    Action action = () => someObject.SomeMethod();
10
11    // assert
12    action.Should().Throw<SomeException>();
13}

In this example, we’re testing that the SomeMethod() method of the someObject object throws a SomeException. If the exception is not thrown or if a different exception is thrown, the test will fail.

Testing for Specific Exception Messages

In addition to testing for the type of exception that is thrown, you can also test for the specific message of the exception. This is useful if you want to ensure that the exception message is correct and provides helpful information to the user.

To test for a specific exception message, you can use the WithMessage() method in combination with Should().Throw().

C#
 1[Test] // or [Fact] if you're using XUnit
 2public void TestMethod_ShouldThrowExceptionWithSpecificMessage()
 3{
 4    // arrange
 5    SomeObject someObject = new();
 6
 7    // act
 8    Action action = () => someObject.SomeMethod();
 9
10    // assert
11    action.Should().Throw<SomeException>()
12          .WithMessage("This is the expected exception message.");
13}

In this example, we’re testing that the SomeMethod() method throws a SomeException with the message “This is the expected exception message.” If the exception message is different or if a different exception is thrown, the test will fail.


FluentAssertions is a great library for testing exceptions in .NET. It provides a simple and easy to use API for testing exceptions and it can help you write robust and reliable code.

FluentAssertions on GitHub: https://github.com/fluentassertions/fluentassertions

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

The Big Decimal Bugfix with .NET 11

Nov 2, 2026 - 11 min read

The Big Decimal Bugfix with .NET 11

For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026 - 10 min read

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact