How would you test a new POST /orders endpoint? Which kinds of tests would you write?
What the interviewer wants to hear
- Unit tests for business logic such as price calculation and validation
- Integration tests with a real database, for example in Docker
- API tests that check status codes, response body and errors
- Cover edge cases: invalid input, missing auth, duplicates, out of stock
- Mock external services and run tests in CI
Model answer
I would follow the testing pyramid. First, unit tests for the pure logic, like calculating totals, applying discounts and validating input, with dependencies replaced by fakes. Next, integration tests that run the repository code against a real PostgreSQL in a container, so I know the SQL and constraints work. Then a few API tests that send real HTTP requests to the app and check the full behaviour: 201 with the new order and a Location header for valid input, 400 with clear field errors for bad input, 401 without a token, 403 for another user's resources, and 409 when stock runs out. External services like payments would be mocked or pointed at a sandbox. I make tests independent, with fresh data for each, and run them all in CI on every pull request so bugs are caught before merging.
Can you answer it under time pressure?
Take a timed easy round and get scored on every answer.
More easy Backend Developer questions
- How would you design a REST API for a simple blog with posts and comments? Mention URLs, methods and status codes.
- When would you choose a SQL database and when a NoSQL database?
- What is the difference between authentication and authorization? How would you implement both in an API?
- Explain what happens on the backend from the moment a request hits your server until a response is sent.
- What is a database index, and what are its costs? How do you decide which columns to index?
- What is a database transaction, and what do the letters in ACID mean? Give an example where you need one.
- What are logs, metrics and traces, and how would you use each one to run a backend service?
- Why do backend teams package services with Docker, and what makes a good Dockerfile?
- Explain the HTTP status code classes and give examples of codes a REST API commonly returns.