0
0
JUnittesting~5 mins

@Mock annotation in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Mock annotation in JUnit testing?
The @Mock annotation is used to create mock objects. These are fake versions of real objects that simulate behavior for testing without using the actual implementation.
Click to reveal answer
intermediate
How do you initialize mocks annotated with @Mock in a JUnit 5 test class?
You initialize mocks by calling MockitoAnnotations.openMocks(this) in a setup method annotated with @BeforeEach. This prepares the mock objects before each test runs.
Click to reveal answer
intermediate
What is the difference between a mock and a spy in testing?
A mock simulates an object and does not call real methods unless specified. A spy wraps a real object and calls real methods unless stubbed to do otherwise.
Click to reveal answer
beginner
Can you use @Mock without a mocking framework like Mockito?
No, @Mock is part of Mockito and requires it to create mock objects. Without Mockito or a similar framework, @Mock has no effect.
Click to reveal answer
beginner
Why is mocking useful in unit testing?
Mocking helps isolate the unit being tested by replacing dependencies with controlled fake objects. This makes tests faster, more reliable, and focused on the unit's behavior.
Click to reveal answer
What does the @Mock annotation do in a JUnit test?
AMarks a test method to be ignored
BRuns the actual method implementation
CInitializes the test class
DCreates a fake object to simulate behavior
How do you activate mocks annotated with @Mock in JUnit 5?
AUse <code>@RunWith(MockitoJUnitRunner.class)</code>
BCall <code>MockitoAnnotations.openMocks(this)</code> in <code>@BeforeEach</code>
CMocks are active by default
DCall <code>initMocks()</code> in <code>@AfterEach</code>
Which framework provides the @Mock annotation?
ATestNG
BJUnit
CMockito
DSelenium
What is a key benefit of using mocks in unit tests?
AIsolates the unit by replacing dependencies
BRuns tests slower
CRequires real database connections
DMakes tests dependent on network
Which statement about mocks is true?
AMocks simulate objects without calling real methods by default
BMocks always call real methods
CMocks cannot be used in JUnit tests
DMocks are used to style UI components
Explain how the @Mock annotation works and why it is useful in unit testing.
Think about replacing real parts with fake ones to test only what you want.
You got /5 concepts.
    Describe the steps to use @Mock in a JUnit 5 test class.
    Initialization is key before running tests.
    You got /4 concepts.