Mocking is creating fake versions of objects or methods to simulate their behavior during testing. It helps isolate the unit being tested by replacing its dependencies with controlled stand-ins.
Click to reveal answer
beginner
Why do we use mocking to isolate units under test?
Mocking isolates units by removing dependencies on other parts of the system. This ensures tests focus only on the unit's logic, making tests faster, more reliable, and easier to understand.
Click to reveal answer
intermediate
How does mocking improve test reliability?
By replacing real dependencies with mocks, tests avoid failures caused by external systems or unpredictable behavior. This leads to consistent and repeatable test results.
Click to reveal answer
beginner
In JUnit, which library is commonly used for mocking?
Mockito is a popular library used with JUnit to create mock objects easily and define their behavior for unit tests.
Click to reveal answer
intermediate
What is a key benefit of isolating units under test with mocks?
It helps identify bugs in the specific unit quickly without interference from other parts, making debugging simpler and faster.
Click to reveal answer
What does mocking help achieve in unit testing?
AIsolate the unit under test by replacing dependencies
BIncrease the size of the test suite
CMake tests dependent on external systems
DRemove the need for assertions
✗ Incorrect
Mocking replaces dependencies with controlled stand-ins, isolating the unit under test.
Which library is commonly used with JUnit for mocking?
ATestNG
BSelenium
CJUnitParams
DMockito
✗ Incorrect
Mockito is widely used with JUnit to create mock objects.
How does mocking improve test speed?
ABy running tests in parallel
BBy avoiding real dependencies that may be slow
CBy skipping test execution
DBy increasing code coverage
✗ Incorrect
Mocks avoid slow real dependencies, making tests run faster.
What is a risk if you do NOT use mocking in unit tests?
ATests may fail due to unrelated external issues
BTests will run faster
CTests will have fewer assertions
DTests will be easier to write
✗ Incorrect
Without mocking, tests can fail because of problems in dependencies, not the unit itself.
What does isolating a unit under test help with?
AMaking tests dependent on network
BIncreasing the number of dependencies
CFinding bugs faster in that unit
DReducing test coverage
✗ Incorrect
Isolation helps focus on the unit, making bug detection quicker.
Explain why mocking is important to isolate units under test in JUnit.
Think about how mocks replace real parts to keep tests simple and focused.
You got /4 concepts.
Describe how mocking helps improve debugging during unit testing.
Consider how removing outside influences helps find problems faster.
You got /4 concepts.
Practice
(1/5)
1. Why do we use mocking in JUnit tests? Mocking helps to:
easy
A. Replace the entire application with a mock
B. Isolate the unit under test by replacing its dependencies with fake objects
C. Automatically generate test data without writing code
D. Make the code run slower to catch timing issues
Solution
Step 1: Understand the purpose of mocking
Mocking replaces real dependencies with fake ones to isolate the code being tested.
Step 2: Identify the effect on tests
This isolation makes tests faster and more reliable by controlling external factors.
Final Answer:
Isolate the unit under test by replacing its dependencies with fake objects -> Option B
Quick Check:
Mocking isolates code = D [OK]
Hint: Mocking isolates by faking dependencies [OK]
Common Mistakes:
Thinking mocking slows tests down
Believing mocking generates test data automatically
Confusing mocking with replacing the whole app
2. Which of the following is the correct way to create a mock object using Mockito in JUnit?
easy
A. MyClass mock = new Mock(MyClass);
B. MyClass mock = new MyClass();
C. MyClass mock = mockito.create(MyClass);
D. MyClass mock = Mockito.mock(MyClass.class);
Solution
Step 1: Recall Mockito syntax for mocking
Mockito uses the static method mock() with the class type as argument.
Step 2: Check each option
Only MyClass mock = Mockito.mock(MyClass.class); uses the correct syntax: Mockito.mock(MyClass.class);
Final Answer:
MyClass mock = Mockito.mock(MyClass.class); -> Option D
Quick Check:
Mockito.mock(Class) syntax = A [OK]
Hint: Use Mockito.mock(Class.class) to create mocks [OK]
Common Mistakes:
Using new keyword instead of Mockito.mock
Wrong method names like create()
Incorrect class references without .class
3. Given the following JUnit test code using Mockito:
class ServiceTest {
@Mock
Repository repo;
@InjectMocks
Service service;
@Test
void testGetData() {
when(repo.getData()).thenReturn("mocked data");
String result = service.getData();
assertEquals("mocked data", result);
}
}
What is the main reason this test passes even if the real Repository is not connected to a database?
medium
A. Because repo is a mock that returns controlled data
B. Because the real Repository is used and connected
C. Because service.getData() ignores the repository
D. Because the test does not check any output
Solution
Step 1: Understand the role of @Mock and when()
The repo is mocked to return "mocked data" when getData() is called.
Step 2: Analyze test behavior
The test passes because the mock returns the expected string, independent of any real database connection.
Final Answer:
Because repo is a mock that returns controlled data -> Option A
Quick Check:
Mock returns controlled data = C [OK]
Hint: Mocks return set values, ignoring real dependencies [OK]
Common Mistakes:
Assuming real database is used
Thinking service ignores repository
Believing test lacks assertions
4. Identify the error in this JUnit test using Mockito:
@Test
void testCalculate() {
Calculator calc = Mockito.mock(Calculator.class);
when(calc.add(2, 3)).thenReturn(5);
int result = calc.add(2, 3);
assertEquals(6, result);
}
medium
A. The expected value in assertEquals is incorrect
B. The mock is not created properly
C. The when() method is used incorrectly
D. The test method lacks @Mock annotation
Solution
Step 1: Review mock setup and behavior
The mock is set to return 5 when add(2, 3) is called.
Step 2: Check assertion correctness
The assertion expects 6, but the mock returns 5, causing failure.
Final Answer:
The expected value in assertEquals is incorrect -> Option A
Quick Check:
Assert expected matches mock return = B [OK]
Hint: Assert expected value must match mock return [OK]
Common Mistakes:
Expecting wrong values in assertions
Confusing mock creation syntax
Missing @Mock annotation on mock fields
5. In a complex system, why is mocking dependencies important to isolate the unit under test? Consider a service that calls a database and a web API. Which benefit does mocking these dependencies provide?
hard
A. It automatically fixes bugs in the database and API
B. It makes the service code run slower to simulate real conditions
C. It allows testing the service logic without relying on database or API availability
D. It replaces the need for writing unit tests
Solution
Step 1: Understand dependency isolation
Mocking replaces real dependencies like database and API with controlled fake versions.
Step 2: Identify benefits for testing
This isolation allows testing service logic reliably without depending on external systems' availability or state.
Final Answer:
It allows testing the service logic without relying on database or API availability -> Option C
Quick Check:
Mocking isolates external dependencies = A [OK]
Hint: Mock dependencies to test logic independently [OK]
Common Mistakes:
Thinking mocking slows code
Believing mocking fixes bugs automatically
Confusing mocking with skipping tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.