📜 ⬆️ ⬇️

Sip moquito

Do you know what a mock object is? No ?

What Wikipedia says about this : “In object-oriented programming, a mock object mimics the behavior of a real object in a specified way ...”. It would seem why? Wikipedia continues : “During unit testing, mock objects can simulate the behavior of business objects and business logic, which is sometimes necessary because of the complexity of real behavior”


And what do mock-libraries give to a java-developer? Of course, the convenience of creating and using those same mock-objects!
')
java-source.net lists as many as 7 libraries:

I started to get acquainted with mock-objects from another library, which is not available on java-source.net - Mockito . I just trusted the opinion of javaposse , who recognized the mockito library of the week, as well as the mood of the java-blogosphere and did not give up. You will also appreciate how convenient it is:

Attention! Taking a sip is impossible to stop.

We check the behavior


A mock object will memorize any calls to its methods, so that later you can check which methods your test code called your mock object.
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();
  1. 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();
  2. 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();
  3. 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();
  4. 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();
  5. 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();
  6. 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();
  7. 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();
  8. 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();
  9. 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();
  10. 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();

But what about the stubs?


By default, all methods of the mock object return the default value, false for boolean, 0 for int, empty collections, null for other objects.
Copy Source | Copy HTML
  1. // You can create a mock for a specific class, not just for an interface
  2. LinkedList mockedList = mock (LinkedList. Class );
  3. // stub'ing
  4. when (mockedList. get (0)). thenReturn (" first ");
  5. when (mockedList. get (1)). thenThrow ( new RuntimeException ());
  6. // get " first "
  7. System. out .println (mockedList. get (0));
  8. // get RuntimeException
  9. System. out .println (mockedList. get (1));
  10. // get " null " because get (999) was not defined
  11. System. out .println (mockedList. get (999));

Checking the exact number of calls


Copy Source | Copy HTML
  1. // use mock object
  2. mockedList. add ("once");
  3. mockedList. add ("twice");
  4. mockedList. add ("twice");
  5. mockedList. add ("three times");
  6. mockedList. add ("three times");
  7. mockedList. add ("three times");
  8. // default check that was called 1 times ~ times (1)
  9. verify (mockedList). add ("once");
  10. verify (mockedList, times (1)). add ("once");
  11. // exact number of calls
  12. verify (mockedList, times (2)). add ("twice");
  13. verify (mockedList, times (3)). add ("three times");
  14. // never ~ never () ~ times (0)
  15. verify (mockedList, never ()). add ("never happened");
  16. // at least as maximum
  17. verify (mockedList, atLeastOnce ()). add ("three times");
  18. verify (mockedList, atLeast (2)). add ("five times");
  19. verify (mockedList, atMost (5)). add ("three times");

We parasitize on real objects


You can create a mock-object (more precisely, a spy-object) that will use the real object when calling method. Overloading only the methods you need, a partial mock object, so to speak.
Copy Source | Copy HTML
  1. List list = new LinkedList ();
  2. List spy = spy (list);
  3. // optional, we define only the size () method
  4. when (spy. size ()). then Return (100);
  5. // use real methods
  6. spy. add ("one");
  7. spy. add ("two");
  8. // get "one"
  9. System. out .println (spy. get (0));
  10. // we override the size () method - we get 100
  11. System. out .println (spy. size ());
  12. // we can check
  13. verify (spy). add ("one");
  14. verify (spy). add ("two");

And



Additional literature:

Thank. And yes, in this post the word mock is mentioned 58 times.

Source: https://habr.com/ru/post/72617/


All Articles