JUnit 5 & Mockito Interview Questions Hindi mein

JUnit 5 & Mockito कोर्स के 25 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

Unit Testing Foundations

1. Which property is part of the FIRST principles for good unit tests?

  1. Repeatable
  2. Flaky
  3. Remote
  4. Sequential only
जवाब देखें

जवाब: Repeatable

FIRST stands for Fast, Isolated, Repeatable, Self-checking and Timely.

पाठ: Why Automated Tests

2. Which JUnit module provides the annotations and assertions you write modern tests with?

  1. JUnit Platform
  2. JUnit Vintage
  3. Maven Surefire
  4. JUnit Jupiter
जवाब देखें

जवाब: JUnit Jupiter

Jupiter is the programming and extension model; the Platform launches tests.

पाठ: JUnit Platform, Jupiter and Build Setup

3. In JUnit's assertEquals, what is the conventional argument order?

  1. actual, expected
  2. message, actual
  3. expected, actual
  4. Any order; it makes no difference to the output
जवाब देखें

जवाब: expected, actual

JUnit expects the expected value first; reversing them produces confusing failure messages.

पाठ: Writing Your First Tests

JUnit Jupiter in Depth

4. What does assertAll do differently from separate assertions?

  1. It runs assertions in parallel threads
  2. It ignores failures
  3. It executes all grouped assertions and reports every failure together
  4. It only works with AssertJ
जवाब देखें

जवाब: It executes all grouped assertions and reports every failure together

assertAll collects failures instead of stopping at the first failing assertion.

पाठ: Assertions, Exceptions and AssertJ

5. By default, how many test-class instances does JUnit Jupiter create for a class with five test methods?

  1. One shared instance
  2. Five, one per test method
  3. Two
  4. None; tests are static
जवाब देखें

जवाब: Five, one per test method

The default PER_METHOD lifecycle creates a fresh instance for each test method.

पाठ: Lifecycle, Nested Tests, Tags and Display Names

6. Which source runs a parameterized test once per row of inline comma-separated values?

  1. @CsvSource
  2. @ValueSource
  3. @EnumSource
  4. @NullSource
जवाब देखें

जवाब: @CsvSource

@CsvSource supplies multiple arguments per invocation from inline CSV rows.

पाठ: Parameterized Tests

Advanced JUnit Features

7. What replaced JUnit 4 runners and rules in JUnit Jupiter?

  1. The extension model with @ExtendWith
  2. XML configuration
  3. Abstract base test classes only
  4. System properties
जवाब देखें

जवाब: The extension model with @ExtendWith

Jupiter has a single, composable extension model registered with @ExtendWith and related mechanisms.

पाठ: The Extension Model, @TempDir and @Timeout

8. What happens when assumeTrue(false) is called inside a test?

  1. The test fails
  2. The JVM exits
  3. The assertion is ignored and the test passes
  4. The test is aborted and reported as skipped
जवाब देखें

जवाब: The test is aborted and reported as skipped

Failed assumptions abort the test as skipped rather than failed.

पाठ: Assumptions and Conditional Execution

9. What do you need to make tests that share a resource safe under JUnit parallel execution?

  1. Nothing; JUnit detects shared resources
  2. Static fields
  3. Synchronisation such as @ResourceLock or running them in the same thread
  4. More assertions
जवाब देखें

जवाब: Synchronisation such as @ResourceLock or running them in the same thread

Shared mutable resources must be declared so JUnit serialises access to them.

पाठ: Dynamic Tests, Ordering and Parallel Execution

Mockito Basics

10. Which test double is a simplified working implementation, such as an in-memory repository?

  1. Dummy
  2. Stub
  3. Fake
  4. Spy
जवाब देखें

जवाब: Fake

A fake really works, but takes shortcuts unsuitable for production.

पाठ: Test Doubles: Dummies, Stubs, Fakes, Spies and Mocks

11. What does an unstubbed Mockito mock method returning `List<String>` return by default?

  1. null
  2. An empty list
  3. A list with one element
  4. It throws an exception
जवाब देखें

जवाब: An empty list

Mockito returns sensible empty defaults for collections, Optionals and primitives.

पाठ: Creating Mocks and Stubbing

12. How do you stub a void method to throw an exception in Mockito?

  1. doThrow(ex).when(mock).voidMethod()
  2. when(mock.voidMethod()).thenThrow(ex)
  3. mock.voidMethod().thenThrow(ex)
  4. Void methods cannot be stubbed
जवाब देखें

जवाब: doThrow(ex).when(mock).voidMethod()

when(...) needs a return value; void methods use the doThrow/doReturn family.

पाठ: Argument Matchers and Advanced Stubbing

Verification and Advanced Mockito

13. Which Mockito feature lets you inspect the actual object passed to a mocked method?

  1. ArgumentCaptor
  2. InOrder
  3. thenAnswer
  4. doNothing
जवाब देखें

जवाब: ArgumentCaptor

ArgumentCaptor captures arguments so the test can assert on their contents.

पाठ: Verifying Interactions and Capturing Arguments

14. Why use doReturn(...).when(spy).method() instead of when(spy.method()).thenReturn(...) for spies?

  1. doReturn is faster
  2. when() does not work with Mockito 5
  3. doReturn verifies the call
  4. when(spy.method()) calls the real method during stubbing, which may have side effects or fail
जवाब देखें

जवाब: 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.

पाठ: Spies, Static and Final Mocking

15. According to common mocking guidelines, how should you test code that uses a third-party SDK?

  1. Mock the SDK classes directly everywhere
  2. Never test it
  3. Wrap the SDK in your own interface, mock that interface in unit tests, and integration-test the adapter
  4. Use static mocks for every SDK call
जवाब देखें

जवाब: 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.

पाठ: BDD Style and Mocking Guidelines

Testable Design and Test Quality

16. Why inject a java.time.Clock instead of calling LocalDate.now() directly?

  1. Clock is faster
  2. LocalDate.now() is deprecated
  3. Tests can supply a fixed clock, making time-dependent behaviour deterministic
  4. Clock stores dates in the database
जवाब देखें

जवाब: Tests can supply a fixed clock, making time-dependent behaviour deterministic

An injected Clock makes the current time a controllable dependency.

पाठ: Designing Code That Is Easy to Test

17. What is the most reliable replacement for Thread.sleep when waiting for asynchronous work in a test?

  1. A longer sleep
  2. Polling for the expected condition with a timeout (for example Awaitility)
  3. Removing the assertion
  4. Running the test twice
जवाब देखें

जवाब: 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.

पाठ: Test Smells and Flaky Tests

18. What are the three steps of the TDD cycle?

  1. Red, green, refactor
  2. Plan, code, deploy
  3. Mock, stub, verify
  4. Compile, link, run
जवाब देखें

जवाब: Red, green, refactor

Write a failing test (red), make it pass (green), then improve the code (refactor).

पाठ: Test-Driven Development

Testing Spring Boot Applications

19. Which annotation loads only the web layer for testing a controller with MockMvc?

  1. @WebMvcTest
  2. @SpringBootTest
  3. @DataJpaTest
  4. @JsonTest
जवाब देखें

जवाब: @WebMvcTest

@WebMvcTest configures just the MVC infrastructure and the specified controllers.

पाठ: @SpringBootTest and Test Slices

20. What does @ServiceConnection do in a Spring Boot test with Testcontainers?

  1. It disables the container
  2. It mocks the database
  3. It runs tests in parallel
  4. It automatically configures the application's connection properties from the running container
जवाब देखें

जवाब: It automatically configures the application's connection properties from the running container

@ServiceConnection derives connection details from the container, replacing manual property wiring.

पाठ: Integration Tests with Testcontainers

21. Why test an HTTP client against WireMock rather than mocking the HTTP library?

  1. WireMock is required by Spring
  2. Mocking HTTP libraries is impossible
  3. It exercises real HTTP serialisation, URLs, headers and error handling through the actual client code
  4. WireMock makes tests skip network code
जवाब देखें

जवाब: 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.

पाठ: Testing Repositories and HTTP Clients

Coverage, CI, Case Study and Revision

22. What does a surviving mutant in mutation testing indicate?

  1. The code has a bug
  2. Coverage is 100%
  3. The tests did not detect that change, revealing a gap in what they check
  4. The build is broken
जवाब देखें

जवाब: 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.

पाठ: Coverage and Mutation Testing

23. In a Maven build, which plugin conventionally runs integration test classes named *IT?

  1. Surefire
  2. Failsafe
  3. JaCoCo
  4. Shade
जवाब देखें

जवाब: Failsafe

Failsafe runs integration tests in the integration-test and verify phases.

पाठ: Running Tests in CI

24. In the case study, how is the rule "a declined payment must not send an email" best tested?

  1. A service unit test with mocks that verifies the mailer is never called
  2. A full end-to-end UI test
  3. A JaCoCo report
  4. A database migration
जवाब देखें

जवाब: 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()).

पाठ: Case Study: Testing an Order Service End to End

25. Which Mockito error indicates that a test stubbed a method that was never called?

  1. NullPointerException
  2. WantedButNotInvoked
  3. InvalidUseOfMatchersException
  4. UnnecessaryStubbingException
जवाब देखें

जवाब: UnnecessaryStubbingException

Strict stubs (default with MockitoExtension) report unused stubbings as UnnecessaryStubbingException.

पाठ: Revision and Interview Questions

← कोर्स पर वापस जाएँ