🧪 Testing Java Services — interview questions
12 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.
12 of 12
01A pull request adds a test for `Checkout`: it mocks `Discount`, stubs `apply(6000)` to return 5400, asserts that `checkout.total(6000)` is 5400, and verifies that `apply(6000)` was called. Coverage goes up and the build is green. The rule is 'orders **over** 5000 paise get 10% off', and the real `Discount` says `>=`. What does this test prove, and what would you write instead?What to testeasy0-2y02In one JUnit 5 test class, `rejectsDuplicateEmail` passes when run on its own and fails when the whole class runs. The class keeps a `static List<Customer> saved` for its in-memory repository, and last week someone added `@TestInstance(Lifecycle.PER_CLASS)` so a `@BeforeAll` method could stop being static. Explain the Jupiter lifecycle rule these two changes work against, and how you would fix the class.JUnit 5easy0-2y03A service has 800 unit tests that run in 4 seconds and 40 browser tests that take 25 minutes, and nothing in between. A repository query with a wrong `JOIN` shipped and was caught only by a browser test, three hours after the merge. Name the shape, explain why the unit tests could not have caught it, and say where you would add tests.Test strategymedium0-2y04After a refactor, twelve tests fail with `UnnecessaryStubbingException` even though no assertion changed. A teammate proposes `@MockitoSettings(strictness = Strictness.LENIENT)` on the base class. What is Mockito telling you, why is lenient the wrong default, and what do these failures usually reveal about the tests?Mockitomedium3-5y05A `@DataJpaTest` persists three orders with `TestEntityManager` and asserts on `findById` and a derived query. It is green, but in production the same repository fails: a native query uses `DISTINCT ON`, and an `@Convert`-mapped column reads back wrong. Also, an `@TransactionalEventListener(phase = AFTER_COMMIT)` handler is never invoked in any repository test. Explain all three with what the slice does by default.Spring Boot test slicesmedium3-5y06A Spring Boot service has 20 integration test classes, each with `@Testcontainers` and its own `@Container static PostgreSQLContainer`. The suite takes 6 minutes and most of it is containers starting. How would you restructure it, how do tests stay isolated afterwards, and where does `withReuse(true)` fit?Testcontainersmedium3-5y07A CI job's Spring test phase went from 3 to 14 minutes over six months, while the number of tests grew by only 20%. CPU is high, the database is idle, and there are 60 `@SpringBootTest` and `@WebMvcTest` classes. What do you suspect, how do you measure it, and what changes would you make?Spring Boot test sliceshard5-8y08On Mockito 4, `mock(TaxTable.class)` failed because `TaxTable` is a `final` class; after upgrading to Mockito 5 the same test passes, and a teammate starts using `mockStatic(Instant.class)` to control time. Explain what a Mockito mock is at runtime under each mock maker, why the upgrade changed the result, and what you would say in review about the static mock.Mockitohard5-8y09A shared CI runner keeps running out of disk, and `docker ps -a` shows hundreds of old `postgres:16-alpine` containers. The pipeline sets `TESTCONTAINERS_RYUK_DISABLED=true` because 'Ryuk kept failing on this runner'. Explain how Testcontainers talks to Docker, what Ryuk does and why it is not a shutdown hook, and how you would fix this properly.Testcontainershard5-8y10A pricing module has 92% line coverage, yet a boundary bug shipped: a refactor changed `amount > 5000` to `amount >= 5000` and no test failed. The team proposes raising the coverage gate to 95%. As the engineer responsible for test strategy, explain why that will not help, what mutation testing would have shown and how it works, and how you would introduce it without slowing every build.Test strategysenior8+y11A CI guard asserts that a practice problem's deliberately broken starter code (an unsynchronised `count++` across a thread pool) **fails** its tests. It passed five deploys in a day, then failed a deploy that changed only markdown, reporting that the starter passes every case. Locally it passes six out of six runs. How do you handle this, what mechanism explains it, and under what conditions would you accept a retry?Flaky testssenior8+y12Your `orders-api` has three consumers owned by other teams: a mobile app, a web front end and a billing service. Pact is in place: consumers publish pacts to a broker and your build runs provider verification. Last month a field removal still reached production and broke the mobile app. Explain what provider verification actually checks, what provider states cost, how the break could get through, and how you would make the broker the thing that stops a deploy.Contract testingsenior8+y