Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML // ( ) import static org.mockito.Mockito.*; // - mock- (: List. class - ) List mockedList = mock(List. class ); // mockedList. add ("one"); mockedList.clear(); //, add "one" clear verify(mockedList). add ("one"); verify(mockedList).clear();
Copy Source | Copy HTML
- // You can create a mock for a specific class, not just for an interface
- LinkedList mockedList = mock (LinkedList. Class );
- // stub'ing
- when (mockedList. get (0)). thenReturn (" first ");
- when (mockedList. get (1)). thenThrow ( new RuntimeException ());
- // get " first "
- System. out .println (mockedList. get (0));
- // get RuntimeException
- System. out .println (mockedList. get (1));
- // get " null " because get (999) was not defined
- System. out .println (mockedList. get (999));
Copy Source | Copy HTML
- // use mock object
- mockedList. add ("once");
- mockedList. add ("twice");
- mockedList. add ("twice");
- mockedList. add ("three times");
- mockedList. add ("three times");
- mockedList. add ("three times");
- // default check that was called 1 times ~ times (1)
- verify (mockedList). add ("once");
- verify (mockedList, times (1)). add ("once");
- // exact number of calls
- verify (mockedList, times (2)). add ("twice");
- verify (mockedList, times (3)). add ("three times");
- // never ~ never () ~ times (0)
- verify (mockedList, never ()). add ("never happened");
- // at least as maximum
- verify (mockedList, atLeastOnce ()). add ("three times");
- verify (mockedList, atLeast (2)). add ("five times");
- verify (mockedList, atMost (5)). add ("three times");
Copy Source | Copy HTML
- List list = new LinkedList ();
- List spy = spy (list);
- // optional, we define only the size () method
- when (spy. size ()). then Return (100);
- // use real methods
- spy. add ("one");
- spy. add ("two");
- // get "one"
- System. out .println (spy. get (0));
- // we override the size () method - we get 100
- System. out .println (spy. size ());
- // we can check
- verify (spy). add ("one");
- verify (spy). add ("two");
Source: https://habr.com/ru/post/72617/
All Articles