Test the domain logic with plain JUnit and no Spring context, since constructor injection makes that trivial. Test persistence against a real database in a container rather than an in memory substitute, because dialects differ and migrations need testing too. Use test slices rather than booting the whole application for controller or repository tests, and stub the external API at the HTTP layer so your client code, retries and parsing all run.
Why interviewers ask this
The interviewer wants to know whether your tests catch regressions or just exercise mocks. Preferring a real database in a container over an in memory one, using slices instead of a full context for speed, and mocking at the HTTP boundary rather than mocking your own repository are all signs of a suite people actually trust. Test runtime matters too, since a slow suite gets skipped.
How to structure your answer
- Split the layers and match a test style to each.
- Use a containerized real database and run migrations in the test.
- Stub external HTTP rather than mocking your own client.
- Keep the suite fast and isolated so it stays trusted.
Example answer
Most of the logic gets plain unit tests with no framework at all: constructor injection means I can instantiate the class with test doubles for its collaborators and it runs in milliseconds. For persistence I use a real database of the same engine and version in a container, with migrations applied as part of the test setup, so the migration itself is verified on every run. An in memory database is faster but it lies about dialects and constraints, and I have shipped a query that worked in tests and failed in production because of it. For the web layer I use a slice that loads just the controller and the serialization, not the whole application, because booting everything for every test is what makes suites take twenty minutes. The external API is stubbed at the HTTP level with a mock server, so my client configuration, timeouts, retry logic and JSON mapping are exercised for real. I also reuse containers across the run and roll back each test in a transaction, which keeps things isolated and parallelizable.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- Why not use an in memory database for speed?
- How do you test the retry and timeout behavior of your client?
- What do you assert to catch an N plus one query in a test?
Related java developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions