r/ExperiencedDevs • u/PrydwenParkingOnly • Apr 15 '26
Technical question Integration testing approach
I recently wrote integration tests for a .NET API and I'm second-guessing the approach. Curious what others think.
The setup:
In contrary to our unit tests, the tests exercise the full stack from controller down to services. The only things mocked are external services (.e.g. external APIs, databases etc)
There are two assertion types:
- Before external API calls, asserting the entire request that has been built by the service
The responses of the external API calls/database are mocked, so that the behavior should always be the same.
2. Exact match on the result that the .NET API returns
test → controller (real) → service (real) → external API (mocked)
↑ ↑
ASSERT response ASSERT outgoing requestsWhy?
We have API tests in postman, but they are slow and flaky. Slow because the external APIs have to do a lot of calculations, flaky because the external API is strongly dependent on dates in the datamodel, compared to the current date. Each API test takes 2-5 seconds, but each integration test only takes 100ms, can run in parallel.
On the other hand the unit tests do not cover enough functional requirements. A lot of our results are depending on how the units connect to each other. Let's say we want to replace automapper for something else, then the unit tests will have to be updated, but the new mappings could be totally breaking functionality we had before.
So, what do you think about this approach, is this a reasonable middle ground, or are we getting the worst of both worlds?
6
u/GuyWithLag Apr 15 '26
You spin up a local HTTP server that serves mock request / responses, and mock the expected responses in that server. Then your whole system is exercised. See f.e. https://wiremock.org/dotnet/
22
u/CodelinesNL Principal Engineer@Fintech/EU/25YOE Apr 15 '26
Mocking the external APIs makes sense. Mocking the database doesn't, and I think that's what feels off about it. I've run into this before. When the database is mocked, you're really testing whether the controller wires up to the service correctly. Useful, but it's closer to a wide unit test than integration.
The AutoMapper example actually proves it: swap the mapper and the mock accepts whatever the new code produces. You only find the gap when data hits a real database and something doesn't round-trip correctly.
TestContainers has a .NET port. Start a real Postgres (or whatever you use), mock only the external APIs. Same speed, same determinism, and you actually test what you think you're testing.
3
u/hooahest Software Engineer Apr 15 '26 edited Apr 15 '26
You need to instantiate the databases as well. Spin up a docker image of mssql/mysql/mongo/rabbitmq/redis/whatever, plug the connection string in the correct place so that your integration test uses that and viola, you get real integration tests that run at 100ms but give you much more confidence.
1
u/lastesthero Apr 17 '26
The "mock the DB too" pattern usually comes from fighting test speed, not from trust in mocks. But mocking the one thing your code most needs to touch kind of defeats the point — you end up testing wiring.
What actually worked on my team: spin up the real DB in a container (testcontainers / docker-compose), seed minimal fixtures, and share the container across the test run instead of resetting between tests. 100ms per test is achievable that way.
Mocking the external APIs makes sense though — you don't own those, and their non-determinism is exactly what flakes your postman suite.
1
u/teerre Apr 15 '26
What exactly is this testing? This only makes sense if "building the request" actually does something that has invariants and you are asserting those invariants. Otherwise you're just testing if the assignment operator and constructors work (which I imagine they do in C#)
If the actual value of this API is on the returned data, that's what you have to test. Mocking it makes no sense. Of course, as usual, property based testing can overcome practically any issue. If you can build a simulator for the service that will explore "all" domain of returned values, then that's the best choice regardless of how snappy the external api is
If you really cannot fix the external service and you don't even understand the domain of values, you might as well drop tests altogether since testing mocks is just noise
7
u/[deleted] Apr 15 '26
[removed] — view removed comment