पाठ 25 / 25
Revision and Interview Questions
Recall JUnit and Mockito concepts quickly for exams and interviews.
Cheat sheet
Foundations: unit vs integration vs end-to-end; test pyramid; FIRST; arrange-act-assert; test behaviour, not implementation. JUnit setup: Platform launches, Jupiter is the programming model (JUnit 5, continued in JUnit 6), Vintage runs old tests; junit-bom; Surefire/Gradle useJUnitPlatform(). JUnit API: @Test, @DisplayName, @BeforeEach/@AfterEach, static @BeforeAll/@AfterAll, PER_METHOD instances, @Nested, @Tag, @Disabled with reason; assertEquals(expected, actual), assertThrows, assertAll, AssertJ fluent assertions, isEqualByComparingTo for BigDecimal; @ParameterizedTest with @ValueSource, @CsvSource, @MethodSource, @EnumSource; extensions with @ExtendWith, @TempDir, @Timeout; assumptions skip tests; @TestFactory dynamic tests; parallel execution with @ResourceLock. Mockito: dummies, stubs, fakes, spies, mocks; @Mock, MockitoExtension strict stubs; when().thenReturn/thenThrow/thenAnswer; matchers all-or-nothing; doThrow/doReturn for void methods and spies; verify, times, never, InOrder, ArgumentCaptor; BDDMockito; mockStatic and final mocking with the inline mock maker; do not mock what you do not own or value objects. Design: dependency injection, pure core, injected Clock, few mocks, no sleeps (Awaitility), TDD red-green-refactor. Spring: slices @WebMvcTest, @DataJpaTest, @MockitoBean, Testcontainers with @ServiceConnection, WireMock. Quality: JaCoCo coverage, PIT mutation testing, Surefire/Failsafe in CI.
Common interview questions
Answer each with a short code example.
1. What is the difference between a unit test and an integration test?
2. What are the JUnit Platform, Jupiter and Vintage?
3. @BeforeEach vs @BeforeAll: when do they run and why must @BeforeAll be static by default?
4. How do parameterized tests work? Name three argument sources.
5. What is the difference between a stub, a mock, a spy and a fake?
6. Why does Mockito throw UnnecessaryStubbingException?
7. How do you stub a void method, and why use doReturn with spies?
8. What is an ArgumentCaptor used for?
9. How would you test code that calls LocalDate.now()?
10. @SpringBootTest vs @WebMvcTest vs @DataJpaTest: when would you use each?
11. Why can high code coverage still mean weak tests? How does mutation testing help?
12. What causes flaky tests and how do you fix them?Show a test, not just a definition
Interviewers remember candidates who write a quick, correct test on the whiteboard: arrange, act, assert, a clear name and one meaningful assertion. It demonstrates the habit, not just the vocabulary.
त्वरित जाँच: Which Mockito error indicates that a test stubbed a method that was never called?
- NullPointerException
- WantedButNotInvoked
- InvalidUseOfMatchersException
- UnnecessaryStubbingException
Answer
UnnecessaryStubbingException — Strict stubs (default with MockitoExtension) report unused stubbings as UnnecessaryStubbingException.