Recall & Review
beginner
What is a mock object in unit testing?
A mock object is a fake version of a real object used in tests to simulate behavior and control interactions without using the real object.
Click to reveal answer
beginner
Why do we use mock objects in tests?
We use mock objects to isolate the code being tested, avoid dependencies on external systems, and test specific behaviors easily.
Click to reveal answer
beginner
In JUnit with Mockito, how do you create a mock object?
You create a mock object using Mockito.mock(ClassName.class), for example: UserService mockService = Mockito.mock(UserService.class);
Click to reveal answer
intermediate
What is the difference between a mock and a stub?
A stub provides predefined responses to calls, while a mock also verifies that certain methods were called with expected arguments.
Click to reveal answer
intermediate
How do you verify that a method was called on a mock object in Mockito?
Use Mockito.verify(mockObject).methodName(arguments) to check if the method was called with the expected arguments.
Click to reveal answer
What is the main purpose of using mock objects in unit tests?
✗ Incorrect
Mock objects help isolate the code being tested by simulating dependencies, making tests faster and more reliable.
Which Mockito method creates a mock object?
✗ Incorrect
Mockito.mock(Class.class) creates a mock object of the specified class.
How do you check if a method was called on a mock in Mockito?
✗ Incorrect
Mockito.verify(mock).method() checks if the method was called on the mock object.
What is a stub compared to a mock?
✗ Incorrect
Stubs provide fixed responses; mocks also verify interactions.
Which of these is NOT a benefit of using mock objects?
✗ Incorrect
Mocks do not test real database performance; they simulate dependencies.
Explain what a mock object is and why it is useful in unit testing.
Think about how you can test a part of your code without using the real parts it depends on.
You got /4 concepts.
Describe how you create and verify a mock object method call using Mockito in JUnit.
Remember the two main steps: make the fake object, then check if it was used as expected.
You got /3 concepts.