# Integration test

## What it is

An integration test exercises a boundary where several parts of a system have to
work together. It can call an application through its HTTP interface and check
what it writes to a real database. It can run a job against a queue and assert
that another component consumes the result. The test gives those parts realistic
inputs and checks behaviour that depends on their interaction.

Choose the boundary by asking where the failure could occur. A review might want
to know whether an API maps database errors to the correct client response. That
test needs the HTTP handler, the code that calls the database and a database
that returns the actual error. It can use a smaller environment for other
services that are outside the question. The result is easier to understand when
the test makes its real dependencies and its controlled substitutes explicit.

Integration tests can run within one service or across several services. What
matters is the contract the test proves. A repository test that uses the
database driver, migrations and real transaction handling can be an integration
test even though it runs no browser. A larger user journey adds evidence about
the visible product. Match the scope to the behaviour and keep the reason for
the chosen setup visible in the test.

## Why it matters

Some failures exist only at the seam between components. A driver may encode a
timestamp with a different timezone from the one the application expects. A
database can reject a concurrent update after both request handlers made it
through their in-memory validation. A queue worker may see a change only after
the sender commits. A test that replaces that dependency with a simple in-memory
response can leave those conditions unobserved.

A realistic boundary also exposes configuration mistakes. A schema may be
missing in the database created by a fresh release, or a real client can send an
omitted field in a different way from the application's fixture. Running the
integration in a repeatable environment shows both the outcome and the
configuration necessary to get it. Test a new installation when the application
can create one, and add a representative upgrade fixture when existing data
affects the decision.

There is a cost to that realism. It takes longer to provision a dependency and
the test suite needs a way to clean up state. A focused integration suite spends
that cost on the parts that have to agree. Keep separate tests for inexpensive
pure logic so failures can still be diagnosed quickly. A good integration result
lets a maintainer identify the relevant request, boundary and state when a
change causes the test to fail.

## What it looks like in practice

Consider an API that reserves a limited item. Start the database with the same
migrations used by the application, load a small inventory with a known balance
and call the route with a valid request. Assert the response, the reservation
record and the resulting balance. Then issue the same operation against an
exhausted item. Check the documented error result and confirm that the database
contains no partial reservation.

Give the fixture a known starting state. An isolated schema, database or
container avoids accidental dependencies on a previous test. Control the clock
where deadlines matter. Record the response, supporting database state and any
notices from the dependency if the assertion fails. This is more useful than an
opaque report that an HTTP request returned the wrong number. It lets a reviewer
see where the disagreement started.

Engineer's
[Go, SQL and browser testing case study](/portfolio/built-a-test-suite-across-go-sql-and-browser/)
describes testing stored functions where the database executes them and using
other suites for API and browser behaviour. The hypothetical reservation test
follows that reasoning across a specific seam. Use real effects where the
outcome depends on them and keep the fixture small enough that the tested
invariant remains obvious. A browser journey can then check the response readers
actually see without carrying every database scenario through the interface.

## Where it goes wrong

Tests can depend on order without saying so. A previous test might leave a
customer, a schema mutation or a queued job that changes the next one's result.
Re-run an individual failure by itself and verify the starting conditions. Use
fixtures whose identifiers and data are deliberately scoped to the test, and
make cleanup reliable even after an assertion or dependency fails.

A wait based on a fixed sleep can also make a healthy operation fail
intermittently. Wait for the event that establishes readiness or completion,
with an explicit deadline and useful diagnostics on timeout. A database
readiness check might need to prove it can accept a query after migrations. A
queue test can wait for the completion record of the specific job it submitted.

Use versions that reproduce the contract you intend to support. A fixture that
always starts the newest database can miss a problem with an older supported
version. A fixture copied from a developer's machine can hide state that a
release is required to create. Name the supported dependency version or image in
the test record. Add an explicit scenario with the schema and data from before a
migration when upgrades are part of the product's normal route.

Finally, make each assertion show the behaviour the integration was chosen to
prove. A green request can coexist with database state that a later operation
cannot read. Check the effect and the application response together where both
matter. Retain a representative failing path and exercise it as the integration
changes. The suite's value comes from being able to explain an actual agreement
between parts of the system and how a reader will know when it breaks.

## Further reading

- [ASP.NET Core integration testing](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests).
  Describes hosting an application and exercising the request path in tests.
- [Testcontainers for Java](https://java.testcontainers.org/). Documents
  disposable service dependencies for tests.
- [Go HTTP testing utilities](https://pkg.go.dev/net/http/httptest). Shows
  standard-library tools for HTTP fixtures and test servers.

<https://engineer.company/glossary/integration-test/>
