What is Mocking?
In unit testing, what is the primary purpose of using mocking?
- To replace real dependencies with controlled substitutes.
- To speed up the execution of unit tests.
- To ensure that the code is written in a specific style.
- To deploy the code to production.
- To improve code coverage reports.
Benefits of Mocking
Which of the following is NOT a primary benefit of using mocks in unit testing?
- Isolating the unit under test from external dependencies.
- Verifying interactions with dependencies.
- Speeding up test execution.
- Writing more comprehensive integration tests.
- Simulating various dependency states and behaviors.
Types of Test Doubles
Which type of test double focuses on verifying that a method was called with the expected arguments?
- Stub
- Mock
- Fake
- Spy
- Dummy
Mocking Frameworks
Which of the following is a popular mocking framework commonly used in Java?
- Mockito
- Jest
- Enzyme
- Selenium
- JUnit
When to Mock
When should you typically consider mocking a dependency in a unit test?
- When the dependency is slow or unreliable.
- When the dependency's behavior is not relevant to the unit being tested.
- When the dependency accesses external resources like a database or network.
- All of the above.
- Only when the dependency throws an exception.
Mocking Return Values
In mocking, what is a common technique to define the return value of a mocked method?
- Using a 'when...thenReturn' construct.
- Relying on the dependency's default behavior.
- Ignoring the return value altogether.
- Deleting the return value from the code
- Re-writing the original code to fit the mock
Mocking Void Methods
How can you verify that a void method on a mock object was called?
- By using a verification method provided by the mocking framework.
- By checking if the method returned null.
- Void methods cannot be mocked or verified.
- By throwing an exception within the mock.
- By printing to the console within the method.
State vs. Behavior Verification
Which of the following BEST describes the difference between state verification and behavior verification in testing with mocks?
- State verification checks the final state of an object, while behavior verification checks the interactions between objects.
- State verification is only for integration tests, while behavior verification is for unit tests.
- State verification is faster than behavior verification.
- State verification is not possible with mocks.
- Behavior verification only tests private methods; State verification tests public methods.
Over-Mocking
What is a common anti-pattern associated with excessive mocking?
- Over-mocking, which can lead to tests that are too tightly coupled to the implementation details.
- Under-mocking, which can lead to tests that are too complex.
- Mocking only data access objects, resulting in poor test coverage.
- Using too many stubbs, which duplicate code.
- Avoiding the use of mocks altogether.
Interface-Based Design
Why is designing with interfaces helpful when using mocks?
- Interfaces allow you to easily substitute implementations with mocks.
- Interfaces automatically create mock objects.
- Interfaces make your tests run faster.
- Interfaces prevent you from writing unit tests
- Interfaces are only useful in integration tests.