Subcutaneous integration tests in .NET Core
Running subcutaneous integration tests for your Application Layer in Clean Architecture based applications.
Quick disclaimer : this post is a continuation of the post from last week on Integration testing, so you can check that one out for additional/alternative tricks and setup.
Previously we have covered how you can set up API layer integration tests. In this issue let’s see how we can set-up Subcutaneous layer integration tests.
Martin Fowler describes subcutaneous testing as : “operates just under the UI of an application”. Essentially we are going to bypass the Routing/Presentation layer and test our Application Layer directly.
As everything in software industry this approach has benefits and tradeoffs:
+ Useful when testing through the UI/Routing/Presentation layer is difficult
- Leaves out the before mentioned layer untouched, meaning you will have to test it some other way.
Setting things up:
Just like in the previous chapters we are going to create a simple dotnet test project (using xUnit), the setup will look like this:
The NuGet packages that we will need this time around are:
FluentAssertions
Microsoft.AspNetCore.Mvc.Testing
EphemeralMongo.Core (https://github.com/asimmon/ephemeral-mongo)
In the previous chapter we have used TestContainers to run our database, this time around I decided to use a different approach (EphemeralMongo) which is also valid and really high performing. And one I used extensively in one of my previous projects.
The general infrastructure setup is really similar to the one we covered last week i.e. by using the WebApplicationFactory class provided by Microsoft.AspNetCore.Mvc.Testing.
In the configure web host method we are going to overwrite the setup of the EfCore. The difference now being that we are not going to use docker and containers to run the databases on demand but rather do it via a custom MongoRunnerProvider.
!!! The code for the MongoRunnerProvider is too long to enter it here so you can grab it from the code base or from the documentation of Ephemeral Mongo, but you will have to adjust it somewhat.
With all that in place we can create a simple test that would validate the behavior of the CreateCardGameCommand run directly via Mediatr. The whole thing would look like this:
And just like that you can run Integration tests directly against your Application Layer.
I feel the need to mention this :
If you decide to go the route of subcutaneous testing you will need to somehow test the UI/Routing/Presentation/API (call it whatever you want) layer.
Personally I don’t trust people enough (jokingly ofc) to leave a part of the system unverified and sleep soundly at night… everyone cut’s corners at some point :)
So if any logic slips into that layer you will not be able to catch it via subcutaneous tests that operate just underneath it. Depending on how this whole newsletter goes on I will try to cover Contract testing at some point in the future which might help with this issue.
No that note, that was it for this issue, it was a smaller one since we mostly built on top of what we covered in the previous issues.
If you liked this kind of content/tutorial I will be really grateful if you cloud share it among your colleagues and friends in order to grow the newsletter further.
Heads up:
I just release an entire course around the topic of building a Modular Monolith system that you can find clicking here : Microservices Ready Architecture
Become a Patreon and get access to the source code presented in the newsletters and YouTube channel.
And with that cya next time :-)





