Bird
Raised Fist0
JUnittesting~20 mins

verify() for interaction verification in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mockito Interaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this JUnit test using Mockito's verify()?
Consider the following JUnit test code snippet using Mockito. What will be the test result after execution?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

class Service {
    void process() {}
}

public class VerifyTest {
    @Test
    void testVerifyCalledOnce() {
        Service service = mock(Service.class);
        service.process();
        verify(service).process();
    }
}
ATest passes successfully
BTest fails with WantedButNotInvoked exception
CTest fails with TooManyActualInvocations exception
DTest fails with NullPointerException
Attempts:
2 left
💡 Hint
Mockito's verify() checks if the method was called exactly once by default.
❓ assertion
intermediate
2:00remaining
Which verify() assertion checks that a method was never called?
In Mockito, which verify() usage correctly asserts that the method process() was never called on the mock?
JUnit
Service service = mock(Service.class);
// no call to service.process() here
Averify(service, times(0)).process();
BverifyNoMoreInteractions(service);
Cverify(service).process();
Dverify(service, never()).process();
Attempts:
2 left
💡 Hint
Check the Mockito documentation for verifying zero invocations.
🔧 Debug
advanced
2:00remaining
Why does this verify() call fail with TooManyActualInvocations?
Given the code below, why does the verify() call fail with a TooManyActualInvocations exception?
JUnit
Service service = mock(Service.class);
service.process();
service.process();
verify(service).process();
ABecause verify() requires specifying times(2) explicitly
BBecause process() was never called
CBecause process() was called twice but verify() expects exactly one call by default
DBecause the mock was not initialized properly
Attempts:
2 left
💡 Hint
Check how many times process() was called versus what verify() expects.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of verifyNoMoreInteractions() in Mockito?
Select the best description of what verifyNoMoreInteractions(mock) does in Mockito tests.
AIt resets the mock to remove all previous interactions
BIt checks that no other methods besides those already verified were called on the mock
CIt verifies that the mock was never called at all
DIt verifies that all methods on the mock were called at least once
Attempts:
2 left
💡 Hint
Think about verifying that no unexpected calls happened.
❓ framework
expert
3:00remaining
In a JUnit 5 test using Mockito, which verify() usage correctly checks a method was called exactly twice with specific arguments?
Given a mock of Service with method process(String input), which verify() call correctly asserts process("data") was called exactly twice?
JUnit
Service service = mock(Service.class);
service.process("data");
service.process("data");
Averify(service, times(2)).process("data");
Bverify(service).process("data"); verify(service).process("data");
Cverify(service, atLeast(2)).process("data");
Dverify(service, times(1)).process(anyString());
Attempts:
2 left
💡 Hint
Check how to specify exact call counts with arguments in verify().

Practice

(1/5)
1. What is the main purpose of verify() in JUnit testing with mocks?
easy
A. To create a mock object
B. To check if a method was called on a mock object
C. To assert the return value of a method
D. To initialize test data before tests run

Solution

  1. Step 1: Understand the role of verify()

    verify() is used to check interactions with mock objects, specifically if certain methods were called.
  2. Step 2: Differentiate from other mock-related actions

    Creating mocks is done by mock(), asserting return values is done by assertEquals(), and initializing data is unrelated to verify().
  3. Final Answer:

    To check if a method was called on a mock object -> Option B
  4. Quick Check:

    verify() checks method calls = B [OK]
Hint: Remember: verify() confirms method calls on mocks [OK]
Common Mistakes:
  • Confusing verify() with mock() creation
  • Using verify() to check return values
  • Thinking verify() initializes test data
2. Which of the following is the correct syntax to verify that method save() was called exactly once on a mock object repository?
easy
A. verify(repository).times(1).save();
B. verify(repository).save(1);
C. verify(repository, times(1)).save();
D. verify(repository).calledOnce(save());

Solution

  1. Step 1: Recall correct verify() syntax

    The correct syntax uses verify(mockObject, times(n)).method() to check method call count.
  2. Step 2: Match options to syntax

    verify(repository, times(1)).save(); matches the correct syntax. Options B, C, and D use invalid or non-existent methods.
  3. Final Answer:

    verify(repository, times(1)).save(); -> Option C
  4. Quick Check:

    verify(mock, times(1)).method() = A [OK]
Hint: Use verify(mock, times(1)).method() to check single call [OK]
Common Mistakes:
  • Writing times(1) after method call
  • Passing call count as method argument
  • Using non-existent verify methods
3. Given the following code snippet, what will happen when the test runs?
MyService service = mock(MyService.class);
service.process();
verify(service, times(2)).process();
medium
A. Test fails because process() was called only once
B. Test passes because verify() ignores call count
C. Test fails due to syntax error in verify()
D. Test passes because process() was called twice

Solution

  1. Step 1: Count method calls on mock

    The method process() is called once on service.
  2. Step 2: Check verify() expectation

    The verify expects process() to be called twice (times(2)), but it was called once.
  3. Final Answer:

    Test fails because process() was called only once -> Option A
  4. Quick Check:

    verify times(2) but called once = D [OK]
Hint: Count actual calls before verify times(n) check [OK]
Common Mistakes:
  • Assuming verify ignores call count
  • Confusing times(2) with atLeast(2)
  • Thinking one call satisfies times(2)
4. Identify the error in this test code snippet:
List<String> list = mock(List.class);
verify(list).add("item");
list.add("item");
medium
A. No error, code is correct
B. Missing times() argument in verify()
C. Incorrect mock creation syntax
D. verify() is called before the method call on the mock

Solution

  1. Step 1: Understand verify() checks past calls

    verify() checks if a method was called before it is invoked.
  2. Step 2: Check order of calls

    Here, verify() is called before list.add("item"), so no call happened yet to verify.
  3. Final Answer:

    verify() is called before the method call on the mock -> Option D
  4. Quick Check:

    verify() must follow method call [OK]
Hint: Always call method before verify() to check it [OK]
Common Mistakes:
  • Calling verify() before method invocation
  • Assuming verify() can predict future calls
  • Ignoring call order in tests
5. You want to verify that a method updateStatus(String status) was called exactly once with argument "ACTIVE" on a mock userService. Which code snippet correctly achieves this?
hard
A. verify(userService, times(1)).updateStatus("ACTIVE");
B. verify(userService).updateStatus(anyString());
C. verify(userService, times(1)).updateStatus();
D. verify(userService, times(1)).updateStatus(anyString());

Solution

  1. Step 1: Use verify() with times(1) for exact call count

    To check exactly one call, use times(1) in verify.
  2. Step 2: Match method call with exact argument

    Pass the exact argument "ACTIVE" inside the method call in verify.
  3. Step 3: Evaluate options

    verify(userService, times(1)).updateStatus("ACTIVE"); matches both call count and argument. verify(userService).updateStatus(anyString()); allows any string, not exact. verify(userService, times(1)).updateStatus(); misses argument. verify(userService, times(1)).updateStatus(anyString()); uses times(1) but anyString() matches any argument, not specifically "ACTIVE".
  4. Final Answer:

    verify(userService, times(1)).updateStatus("ACTIVE"); -> Option A
  5. Quick Check:

    verify with times(1) and exact arg = C [OK]
Hint: Use times(1) and exact argument in verify() [OK]
Common Mistakes:
  • Using anyString() instead of exact argument
  • Omitting times(1) for call count
  • Calling verify() without arguments
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