Mocking Dependencies in Unit Tests: The Ultimate Quiz Quiz

  1. What is Mocking?

    In unit testing, what is the primary purpose of using mocking?

    1. To replace real dependencies with controlled substitutes.
    2. To speed up the execution of unit tests.
    3. To ensure that the code is written in a specific style.
    4. To deploy the code to production.
    5. To improve code coverage reports.
  2. Benefits of Mocking

    Which of the following is NOT a primary benefit of using mocks in unit testing?

    1. Isolating the unit under test from external dependencies.
    2. Verifying interactions with dependencies.
    3. Speeding up test execution.
    4. Writing more comprehensive integration tests.
    5. Simulating various dependency states and behaviors.
  3. Types of Test Doubles

    Which type of test double focuses on verifying that a method was called with the expected arguments?

    1. Stub
    2. Mock
    3. Fake
    4. Spy
    5. Dummy
  4. Mocking Frameworks

    Which of the following is a popular mocking framework commonly used in Java?

    1. Mockito
    2. Jest
    3. Enzyme
    4. Selenium
    5. JUnit
  5. When to Mock

    When should you typically consider mocking a dependency in a unit test?

    1. When the dependency is slow or unreliable.
    2. When the dependency's behavior is not relevant to the unit being tested.
    3. When the dependency accesses external resources like a database or network.
    4. All of the above.
    5. Only when the dependency throws an exception.
  6. Mocking Return Values

    In mocking, what is a common technique to define the return value of a mocked method?

    1. Using a 'when...thenReturn' construct.
    2. Relying on the dependency's default behavior.
    3. Ignoring the return value altogether.
    4. Deleting the return value from the code
    5. Re-writing the original code to fit the mock
  7. Mocking Void Methods

    How can you verify that a void method on a mock object was called?

    1. By using a verification method provided by the mocking framework.
    2. By checking if the method returned null.
    3. Void methods cannot be mocked or verified.
    4. By throwing an exception within the mock.
    5. By printing to the console within the method.
  8. State vs. Behavior Verification

    Which of the following BEST describes the difference between state verification and behavior verification in testing with mocks?

    1. State verification checks the final state of an object, while behavior verification checks the interactions between objects.
    2. State verification is only for integration tests, while behavior verification is for unit tests.
    3. State verification is faster than behavior verification.
    4. State verification is not possible with mocks.
    5. Behavior verification only tests private methods; State verification tests public methods.
  9. Over-Mocking

    What is a common anti-pattern associated with excessive mocking?

    1. Over-mocking, which can lead to tests that are too tightly coupled to the implementation details.
    2. Under-mocking, which can lead to tests that are too complex.
    3. Mocking only data access objects, resulting in poor test coverage.
    4. Using too many stubbs, which duplicate code.
    5. Avoiding the use of mocks altogether.
  10. Interface-Based Design

    Why is designing with interfaces helpful when using mocks?

    1. Interfaces allow you to easily substitute implementations with mocks.
    2. Interfaces automatically create mock objects.
    3. Interfaces make your tests run faster.
    4. Interfaces prevent you from writing unit tests
    5. Interfaces are only useful in integration tests.