1. Which property is part of the FIRST principles for good unit tests?
- Repeatable
- Flaky
- Remote
- Sequential only
Show answer
Answer: Repeatable
FIRST stands for Fast, Isolated, Repeatable, Self-checking and Timely.
Lesson: Why Automated Tests
25 questions from the JUnit 5 & Mockito course, each with the correct answer and an explanation. Try to answer before you open the answer.
Answer: Repeatable
FIRST stands for Fast, Isolated, Repeatable, Self-checking and Timely.
Lesson: Why Automated Tests
Answer: JUnit Jupiter
Jupiter is the programming and extension model; the Platform launches tests.
Answer: expected, actual
JUnit expects the expected value first; reversing them produces confusing failure messages.
Lesson: Writing Your First Tests
Answer: It executes all grouped assertions and reports every failure together
assertAll collects failures instead of stopping at the first failing assertion.
Answer: Five, one per test method
The default PER_METHOD lifecycle creates a fresh instance for each test method.
Answer: @CsvSource
@CsvSource supplies multiple arguments per invocation from inline CSV rows.
Lesson: Parameterized Tests
Answer: The extension model with @ExtendWith
Jupiter has a single, composable extension model registered with @ExtendWith and related mechanisms.
Answer: The test is aborted and reported as skipped
Failed assumptions abort the test as skipped rather than failed.
Answer: Synchronisation such as @ResourceLock or running them in the same thread
Shared mutable resources must be declared so JUnit serialises access to them.
Answer: Fake
A fake really works, but takes shortcuts unsuitable for production.
Lesson: Test Doubles: Dummies, Stubs, Fakes, Spies and Mocks
Answer: An empty list
Mockito returns sensible empty defaults for collections, Optionals and primitives.
Lesson: Creating Mocks and Stubbing
Answer: doThrow(ex).when(mock).voidMethod()
when(...) needs a return value; void methods use the doThrow/doReturn family.
Answer: ArgumentCaptor
ArgumentCaptor captures arguments so the test can assert on their contents.
Answer: when(spy.method()) calls the real method during stubbing, which may have side effects or fail
Spies run real code, so the doReturn style avoids executing the real method while stubbing.
Lesson: Spies, Static and Final Mocking
Answer: Wrap the SDK in your own interface, mock that interface in unit tests, and integration-test the adapter
Owning the interface keeps unit tests stable and isolates SDK assumptions in a tested adapter.
Lesson: BDD Style and Mocking Guidelines
Answer: Tests can supply a fixed clock, making time-dependent behaviour deterministic
An injected Clock makes the current time a controllable dependency.
Answer: Polling for the expected condition with a timeout (for example Awaitility)
Polling waits exactly as long as needed, up to a bound, removing timing guesses.
Lesson: Test Smells and Flaky Tests
Answer: Red, green, refactor
Write a failing test (red), make it pass (green), then improve the code (refactor).
Lesson: Test-Driven Development
Answer: @WebMvcTest
@WebMvcTest configures just the MVC infrastructure and the specified controllers.
Lesson: @SpringBootTest and Test Slices
Answer: It automatically configures the application's connection properties from the running container
@ServiceConnection derives connection details from the container, replacing manual property wiring.
Answer: It exercises real HTTP serialisation, URLs, headers and error handling through the actual client code
A local stub server tests the real request and response handling end to end.
Answer: The tests did not detect that change, revealing a gap in what they check
A surviving mutant means no test failed when the behaviour was altered.
Lesson: Coverage and Mutation Testing
Answer: Failsafe
Failsafe runs integration tests in the integration-test and verify phases.
Lesson: Running Tests in CI
Answer: A service unit test with mocks that verifies the mailer is never called
Interaction-focused behaviour of the service is cheapest to check with Mockito and verify(never()).
Answer: UnnecessaryStubbingException
Strict stubs (default with MockitoExtension) report unused stubbings as UnnecessaryStubbingException.
Lesson: Revision and Interview Questions