Guest User

Untitled

a guest
May 21st, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import static org.mockito.Mockito.*;
  2.  
  3. @Test
  4. public void testVerify() {
  5. // create and configure mock
  6. MyClass test = Mockito.mock(MyClass.class);
  7. when(test.getUniqueId()).thenReturn(43);
  8.  
  9.  
  10. // call method testing on the mock with parameter 12
  11. test.testing(12);
  12. test.getUniqueId();
  13. test.getUniqueId();
  14.  
  15.  
  16. // now check if method testing was called with the parameter 12
  17. verify(test).testing(ArgumentMatchers.eq(12));
  18.  
  19. // was the method called twice?
  20. verify(test, times(2)).getUniqueId();
  21.  
  22. // other alternatives for verifiying the number of method calls for a method
  23. verify(test, never()).someMethod("never called");
  24. verify(test, atLeastOnce()).someMethod("called at least once");
  25. verify(test, atLeast(2)).someMethod("called at least twice");
  26. verify(test, times(5)).someMethod("called five times");
  27. verify(test, atMost(3)).someMethod("called at most 3 times");
  28. // This let's you check that no other methods where called on this object.
  29. // You call it after you have verified the expected method calls.
  30. verifyNoMoreInteractions(test);
  31. }
Add Comment
Please, Sign In to add comment