Bird
Raised Fist0
JUnittesting~10 mins

Why advanced mocking handles complex dependencies in JUnit - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a mock object using Mockito.

JUnit
MyService service = Mockito.[1](MyService.class);
Drag options to blanks, or click blank then click option'
Amock
Bspy
Cverify
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spy' instead of 'mock' creates a partial mock, not a full mock.
Using 'verify' or 'when' does not create mocks.
2fill in blank
medium

Complete the code to stub a method call to return a specific value.

JUnit
Mockito.[1](service.getData()).thenReturn("mocked data");
Drag options to blanks, or click blank then click option'
Aspy
Bverify
Cmock
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'verify' instead of 'when' is for checking calls, not stubbing.
Using 'mock' or 'spy' here is incorrect.
3fill in blank
hard

Fix the error in verifying that a method was called exactly once.

JUnit
Mockito.verify(service, Mockito.[1]()).process();
Drag options to blanks, or click blank then click option'
Atimes(2)
Btimes(1)
Conce()
Dnever()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once()' causes a compilation error because it is not a valid Mockito method.
Using 'times(2)' checks for two calls, not one.
4fill in blank
hard

Fill both blanks to mock a complex dependency and stub its method.

JUnit
Dependency dep = Mockito.[1](Dependency.class);
Mockito.[2](dep.getValue()).thenReturn(42);
Drag options to blanks, or click blank then click option'
Amock
Bwhen
Cspy
Dverify
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spy' instead of 'mock' creates a partial mock, which may not be desired.
Using 'verify' instead of 'when' is incorrect for stubbing.
5fill in blank
hard

Fill all three blanks to verify a method call with argument matcher and times.

JUnit
Mockito.verify(service, Mockito.[1](3)).execute(Mockito.[2]("test"), Mockito.[3]());
Drag options to blanks, or click blank then click option'
Atimes
Beq
Cany
Dnever
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'never' instead of 'times' causes verification to expect zero calls.
Using 'any' instead of 'eq' loses exact argument matching.

Practice

(1/5)
1. Why do we use advanced mocking in JUnit tests when dealing with complex dependencies?
easy
A. To automatically generate user interfaces
B. To isolate the code under test and control the behavior of its dependencies
C. To avoid writing any test cases
D. To make the code run faster in production

Solution

  1. Step 1: Understand the purpose of mocking

    Mocking replaces real dependencies with fake ones to isolate the code being tested.
  2. Step 2: Recognize the benefit of advanced mocking

    Advanced mocking allows control over complex dependencies, making tests reliable and focused.
  3. Final Answer:

    To isolate the code under test and control the behavior of its dependencies -> Option B
  4. Quick Check:

    Mocking isolates dependencies [OK]
Hint: Mocking isolates dependencies to test code alone [OK]
Common Mistakes:
  • Thinking mocking speeds up production code
  • Believing mocking removes the need for tests
  • Confusing mocking with UI generation
2. Which of the following is the correct syntax to create a mock object using Mockito in a JUnit test?
easy
A. MyClass mock = Mockito.mock(MyClass.class);
B. MyClass mock = new MyClass();
C. MyClass mock = mockito.create(MyClass);
D. MyClass mock = Mockito.createMock(MyClass);

Solution

  1. Step 1: Recall Mockito mock creation syntax

    Mockito uses the static method mock(Class) to create mock objects.
  2. Step 2: Verify the correct syntax

    The correct syntax is: MyClass mock = Mockito.mock(MyClass.class);
  3. Final Answer:

    MyClass mock = Mockito.mock(MyClass.class); -> Option A
  4. Quick Check:

    Mockito.mock(Class) creates mocks [OK]
Hint: Use Mockito.mock(Class.class) to create mocks [OK]
Common Mistakes:
  • Using new keyword instead of Mockito.mock
  • Calling non-existent Mockito methods
  • Incorrect class reference syntax
3. Given the following JUnit test code using Mockito:
@ExtendWith(MockitoExtension.class)
class ServiceTest {
@Mock
Repository repo;

@InjectMocks
Service service;

@Test
void testGetData() {
when(repo.fetch()).thenReturn("data");
String result = service.getData();
assertEquals("data", result);
}
}

What will be the test result if repo.fetch() is called inside service.getData()?
medium
A. Test fails because repo.fetch() returns null
B. Test fails due to syntax error in mocking
C. Test throws NullPointerException due to uninitialized repo
D. Test passes because repo.fetch() returns mocked "data"

Solution

  1. Step 1: Understand Mockito annotations usage

    @Mock creates a mock for Repository, and @InjectMocks injects it into Service.
  2. Step 2: Analyze the when-thenReturn setup

    Calling repo.fetch() returns "data" as stubbed, so service.getData() returns "data".
  3. Final Answer:

    Test passes because repo.fetch() returns mocked "data" -> Option D
  4. Quick Check:

    Mock returns stubbed value [OK]
Hint: Stub methods with when().thenReturn() to control outputs [OK]
Common Mistakes:
  • Assuming mocks return null by default
  • Forgetting to initialize mocks with @ExtendWith or MockitoAnnotations
  • Confusing mock injection with real object creation
4. Consider this JUnit test snippet using Mockito:
@ExtendWith(MockitoExtension.class)
class TestClass {
@Mock
Database db;

@Test
void testQuery() {
when(db.query("SELECT * FROM users")).thenReturn(null);
String result = db.query("SELECT * FROM users");
assertNotNull(result);
}
}

What is the problem with this test?
medium
A. The assertion will fail because the mock returns null
B. The syntax of when() is incorrect
C. The mock object db is not created properly
D. The test will pass because null is acceptable

Solution

  1. Step 1: Analyze the stubbed return value

    The mock is set to return null for the query method call.
  2. Step 2: Check the assertion logic

    The test asserts that the result is not null, but the stub returns null, so assertion fails.
  3. Final Answer:

    The assertion will fail because the mock returns null -> Option A
  4. Quick Check:

    Assertion contradicts stubbed null [OK]
Hint: Match stubbed return values with assertions [OK]
Common Mistakes:
  • Ignoring that stub returns null
  • Assuming syntax error without checking code
  • Believing test passes with failing assertions
5. You have a service class that depends on two complex external APIs. You want to write a JUnit test that verifies your service logic without calling these APIs. Which advanced mocking approach best handles this scenario?
hard
A. Manually create fake API responses inside the service code
B. Write integration tests that call the real APIs directly
C. Use Mockito to create mocks for both API clients and inject them into the service
D. Skip testing the service since APIs are complex

Solution

  1. Step 1: Identify the need to isolate service from external APIs

    Testing should avoid calling real APIs to keep tests fast and reliable.
  2. Step 2: Choose advanced mocking to simulate API behavior

    Mockito mocks for API clients allow controlling responses and injecting them into the service.
  3. Final Answer:

    Use Mockito to create mocks for both API clients and inject them into the service -> Option C
  4. Quick Check:

    Mock external APIs to isolate service [OK]
Hint: Mock external dependencies to isolate service logic [OK]
Common Mistakes:
  • Testing with real APIs causing slow or flaky tests
  • Hardcoding fake responses inside service code
  • Avoiding tests due to complexity
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard