A Test Double is a simplified object that stands in for a real object during testing to control behavior and isolate tests.
Click to reveal answer
beginner
What is the main purpose of a Dummy object?
A Dummy object is passed around but never actually used. It just fills parameter lists to satisfy method signatures.
Click to reveal answer
intermediate
How does a Stub differ from a Dummy in testing?
A Stub provides predefined responses to calls made during the test, allowing control over indirect inputs, unlike a Dummy which is unused.
Click to reveal answer
intermediate
What role does a Mock play in unit testing?
A Mock not only simulates behavior but also verifies that certain interactions (like method calls) happened as expected during the test.
Click to reveal answer
advanced
Why use different types of test doubles instead of just one?
Different test doubles serve different purposes: Dummies fill parameters, Stubs control indirect inputs, Mocks verify behavior, making tests clearer and more focused.
Click to reveal answer
Which test double is used only to fill parameter lists and is never actually used?
AMock
BStub
CDummy
DSpy
✗ Incorrect
A Dummy object is passed just to fill parameters and is not used in the test logic.
Which test double provides canned responses to method calls during testing?
AStub
BMock
CDummy
DFake
✗ Incorrect
A Stub returns predefined data to control indirect inputs in tests.
Which test double verifies that certain methods were called during the test?
ADummy
BMock
CStub
DFake
✗ Incorrect
Mocks are used to verify interactions, such as method calls, happened as expected.
Why might you choose a Stub over a Mock in a test?
ATo provide canned responses without verifying calls
BTo replace the entire system
CTo fill unused parameters
DTo verify method calls
✗ Incorrect
Stubs provide canned responses but do not verify interactions.
What is the main benefit of using different test doubles?
ATo avoid writing tests
BTo make tests slower
CTo confuse developers
DTo isolate tests and clarify their purpose
✗ Incorrect
Different test doubles help isolate tests and make their purpose clearer.
Explain the differences between Dummy, Stub, and Mock test doubles and when to use each.
Think about how each double helps control or verify behavior in tests.
You got /3 concepts.
Why is it important to choose the right type of test double in unit testing?
Consider how different doubles affect test focus and reliability.
You got /4 concepts.
Practice
(1/5)
1. Which test double is primarily used to provide predefined data to the code under test without verifying interactions?
easy
A. Stub
B. Mock
C. Spy
D. Fake
Solution
Step 1: Understand the role of a stub
A stub provides fixed data or responses to the code being tested but does not check if methods were called.
Step 2: Differentiate from mocks and spies
Mocks verify interactions, spies do both spying and stubbing, and fakes are simplified implementations. Only stubs focus on providing data without verification.
Final Answer:
Stub -> Option A
Quick Check:
Stub = provides data only [OK]
Hint: Stubs give data, mocks check calls, spies do both [OK]
Common Mistakes:
Confusing mocks with stubs
Thinking spies only provide data
Mixing fakes with stubs
2. Which of the following is the correct way to create a mock object using Mockito in JUnit?
easy
A. MyClass mock = spy(new MyClass());
B. MyClass mock = new MyClass();
C. MyClass mock = Mockito.mock(MyClass.class);
D. MyClass mock = new FakeMyClass();
Solution
Step 1: Recall Mockito syntax for mocks
Mockito.mock(Class.class) creates a mock object for the given class.
Step 2: Identify incorrect options
MyClass mock = new MyClass(); creates a real object, not a mock. MyClass mock = spy(new MyClass()); creates a spy, not a mock. MyClass mock = new FakeMyClass(); uses a fake class, not a mock.
Final Answer:
MyClass mock = Mockito.mock(MyClass.class); -> Option C
Quick Check:
Mockito.mock() creates mocks [OK]
Hint: Use Mockito.mock(Class.class) to create mocks [OK]
Common Mistakes:
Using new keyword instead of Mockito.mock
Confusing spy() with mock()
Assuming fake classes are mocks
3. Given the following code snippet using Mockito:
List<String> list = Mockito.spy(new ArrayList<>());
list.add("one");
Mockito.verify(list).add("one");
System.out.println(list.size());
What will be printed?
medium
A. 0
B. null
C. Throws an exception
D. 1
Solution
Step 1: Understand spy behavior
A spy wraps a real object and records interactions but calls real methods unless stubbed.
Step 2: Analyze the code execution
Adding "one" to the spy list calls the real add method, so the list size becomes 1. The verify confirms the add call happened.
Final Answer:
1 -> Option D
Quick Check:
Spy calls real methods and records calls [OK]
Hint: Spy calls real methods and tracks calls [OK]
Common Mistakes:
Thinking spy does not call real methods
Assuming size remains 0 after add
Expecting null or exception output
4. Identify the error in this Mockito test code:
MyService service = Mockito.mock(MyService.class);
service.process();
Mockito.verify(service.process());
medium
A. Missing MockitoAnnotations.initMocks(this) call
B. verify() should receive a method call without parentheses
C. verify() should be called before process()
D. process() method cannot be mocked
Solution
Step 1: Understand Mockito verify syntax
Mockito.verify() expects the mock object as argument, then the method call without parentheses inside verify.
Step 2: Identify the mistake in code
The code calls verify(service.process()), which calls process() immediately and passes its result. Correct is verify(service).process().
Final Answer:
verify() should receive a method call without parentheses -> Option B
Quick Check:
Use verify(mock).method() syntax [OK]
Hint: Use verify(mock).method() without parentheses inside verify [OK]
Common Mistakes:
Calling method inside verify() parentheses
Calling verify before method execution
Confusing mock initialization with verification
5. You want to test a method that calls an external payment service. You need to check that the payment method was called with correct parameters, but also want to return a fixed success response. Which test double should you use?
hard
A. Mock
B. Spy
C. Stub
D. Fake
Solution
Step 1: Understand requirements for the test double
You want to verify the method call (like a mock) and also provide a fixed response (like a stub).
Step 2: Identify the double that combines both features
A mock verifies interactions and can return fixed responses when configured with when(mock.method(params)).thenReturn(success).
Final Answer:
Mock -> Option A
Quick Check:
Mock = verify calls + stub data [OK]
Hint: Mock verifies calls and stubs responses [OK]
Common Mistakes:
Using stub which can't verify calls
Using spy which executes real methods unless stubbed
Confusing fake with mock
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.