0
0
JUnittesting~5 mins

Mock objects in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ATo isolate the unit under test from external dependencies
BTo slow down the test execution
CTo replace the entire application
DTo write production code
Which Mockito method creates a mock object?
AMockito.mock()
BMockito.create()
CMockito.fake()
DMockito.stub()
How do you check if a method was called on a mock in Mockito?
AMockito.check()
BMockito.call()
CMockito.assert()
DMockito.verify()
What is a stub compared to a mock?
AThey are exactly the same
BA stub verifies method calls, a mock provides responses
CA stub provides predefined responses, a mock verifies method calls
DA stub is used only in integration tests
Which of these is NOT a benefit of using mock objects?
AFaster test execution
BTesting real database performance
CIsolating tests from external systems
DControlling test scenarios easily
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.