API-Level End-to-End Testing

API-level end-to-end tests exercise complete backend workflows through service interfaces rather than a browser UI. They verify that multiple backend components work together correctly from an external API request through the resulting system state.

An API E2E test treats the backend as a system under test and interacts with it through its API boundary. A typical test might create a customer, authenticate, create an order, retrieve the order, and verify that the expected state was persisted.

Unlike a unit test, an API E2E test usually allows multiple real application layers to participate, such as controllers, validation, business services, persistence, and sometimes external dependencies. The exact scope depends on the test environment and which dependencies are replaced with test doubles.

A good API E2E test focuses on a meaningful business workflow rather than implementation details. Assertions should verify observable behavior, including HTTP status codes, response data, important headers, and relevant persisted state.

These tests are commonly slower and more failure-prone than unit tests because they exercise more infrastructure. They therefore complement rather than replace unit and integration tests. A practical test suite keeps API E2E coverage focused on critical workflows and uses narrower tests for detailed edge cases.

Key points

Interview questions

What would you test in an order-creation API workflow?

I would first establish any required test data and authentication state. Then I would call the order-creation endpoint with a valid request and verify the expected status code and response fields. I would also retrieve or inspect the resulting order to confirm that the important state change actually occurred. Separate tests would cover invalid input, authorization failures, and important business rules.

What is an API-level end-to-end test?

It is a test that exercises a complete backend workflow through an API rather than through a browser interface. The request enters through the API boundary and can pass through validation, business logic, persistence, and other participating services. The test then verifies observable results such as the HTTP response and resulting data. It is useful for checking that several backend pieces work correctly together.

Why not use API E2E tests for every backend test case?

E2E tests exercise many components, so they generally cost more time to execute and can fail because of infrastructure or environment problems. They are also more expensive to diagnose when something breaks because several layers may be involved. Narrower unit and integration tests can cover detailed business rules more quickly and with more precise failures. E2E tests are therefore most valuable for a focused set of important workflows.

How do you keep API E2E tests deterministic?

Each test should control the data and environment it depends on rather than relying on state left by another test. I would use isolated test data, reset or transactional database state where appropriate, and deterministic configuration for dependencies. External systems that are not part of the behavior being tested can be stubbed or replaced with controlled test services. I would also avoid timing-based assertions and use explicit synchronization when asynchronous processing is part of the workflow.

How would you structure API E2E tests in a Java Spring Boot application?

I would define a small set of critical workflows and execute them against an application environment representative of production. Depending on the goal, requests could be sent through MockMvc or a real HTTP client against a running test instance, while infrastructure such as the database can be provided by dedicated test resources. Tests should verify the API contract and meaningful state transitions rather than internal method calls. I would keep setup and cleanup explicit and use isolated test data so failures are reproducible.