1. Which property is part of the FIRST principles for good unit tests?
- Repeatable
- Flaky
- Remote
- Sequential only
जवाब देखें
जवाब: Repeatable
FIRST stands for Fast, Isolated, Repeatable, Self-checking and Timely.
पाठ: Why Automated Tests
JUnit 5 & Mockito कोर्स के 25 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।
जवाब: Repeatable
FIRST stands for Fast, Isolated, Repeatable, Self-checking and Timely.
पाठ: Why Automated Tests
जवाब: JUnit Jupiter
Jupiter is the programming and extension model; the Platform launches tests.
जवाब: expected, actual
JUnit expects the expected value first; reversing them produces confusing failure messages.
जवाब: It executes all grouped assertions and reports every failure together
assertAll collects failures instead of stopping at the first failing assertion.
जवाब: Five, one per test method
The default PER_METHOD lifecycle creates a fresh instance for each test method.
जवाब: @CsvSource
@CsvSource supplies multiple arguments per invocation from inline CSV rows.
पाठ: Parameterized Tests
जवाब: The extension model with @ExtendWith
Jupiter has a single, composable extension model registered with @ExtendWith and related mechanisms.
जवाब: The test is aborted and reported as skipped
Failed assumptions abort the test as skipped rather than failed.
जवाब: Synchronisation such as @ResourceLock or running them in the same thread
Shared mutable resources must be declared so JUnit serialises access to them.
जवाब: Fake
A fake really works, but takes shortcuts unsuitable for production.
जवाब: An empty list
Mockito returns sensible empty defaults for collections, Optionals and primitives.
जवाब: doThrow(ex).when(mock).voidMethod()
when(...) needs a return value; void methods use the doThrow/doReturn family.
जवाब: ArgumentCaptor
ArgumentCaptor captures arguments so the test can assert on their contents.
जवाब: 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.
जवाब: 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.
जवाब: Tests can supply a fixed clock, making time-dependent behaviour deterministic
An injected Clock makes the current time a controllable dependency.
जवाब: 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.
जवाब: Red, green, refactor
Write a failing test (red), make it pass (green), then improve the code (refactor).
जवाब: @WebMvcTest
@WebMvcTest configures just the MVC infrastructure and the specified controllers.
जवाब: It automatically configures the application's connection properties from the running container
@ServiceConnection derives connection details from the container, replacing manual property wiring.
जवाब: 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.
जवाब: 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.
जवाब: Failsafe
Failsafe runs integration tests in the integration-test and verify phases.
पाठ: Running Tests in CI
जवाब: 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()).
जवाब: UnnecessaryStubbingException
Strict stubs (default with MockitoExtension) report unused stubbings as UnnecessaryStubbingException.