Mockito - Beginners

Mockito tool is a mock / frame. I understand EasyMock bit dated, Mockito is now more popular.

What is the mock? He said bluntly, we all know that unit test should be as independent. For a class of unit test should no longer have any interaction with other class.

Now there is a class, a directory and find the file scanning are uploaded to the FTP server. For a different kind of response FTP (FTP server or upload can not find success, or upload failed), there are some follow-up action.

In writing UT this class, we have a fictitious FTP object. In this way the UT, this fictional objects can substitute for real FTP, make some response to the call being tested class. So you know whether the class being tested right calls and make some good FTP desired response. So as to achieve the purpose of the test.

mock can simulate a variety of objects, so instead of the real object to make the desired response.

On the concept and EasyMock mock, you can refer to:

 Mock object and EasyMock framework

http://blog.csdn.net/OnlyQi/archive/2011/04/26/6364885.aspx

 

Official website: http://mockito.org/

 A very good introductory article:

http://blog.csdn.net/huoshuxiao/archive/2010/12/30/6107835.aspx

 Something a little bit complicated and useful examples:

http://gojko.net/2009/10/23/mockito-in-six-easy-examples/

 

The following describes the basic use of mockito.

 As previously mentioned, we need to create a mock object instead of the true object.

therefore,

Mock objects is the first step. Simulation object using the mock ();

The return value is the second step when the designated mock object is called, for example specify that the object is first mockFTP return "can not find FTP server" is called. This step is generally referred to as stubbing. Generally the when (mockedList.get ( 0 )). ThenReturn ( "First" ) appearance.

Verify whether the test is working properly is the third step class, using the verify (). For example, when the authentication object returns mockFTP "not found FTP server", the test code is required to retry.

Then the test is completed friends ~ ~

 

Analog objects:

  1. // simulate an object LinkedList  
  2. LinkedList mockedList = mock(LinkedList.class);   
  3.   
  4. // this time calling the get method will return null, because no return value method calls to do simulation   
  5. System.out.println(mockedList.get(999));  

Simulation method called Return Value:

  such as

  1. When the analog // Get the first element, it returns a string first.   To a particular method call returns a fixed value called a stub in the official parlance.
  2. when(mockedList.get(0)).thenReturn("first");   
  3.   
  4. // At this first printout   
  5. System.out.println(mockedList.get(0));  

Simulation method call throws an exception:

  1. When the simulation // get the second element, throw RuntimeException  
  2. when(mockedList.get(1)).thenThrow(new RuntimeException());   
  3.   
  4. // this time will throw RuntimeException  
  5. System.out.println(mockedList.get(1));  
 No return value may be an analog type of exception thrown method:

doThrow(new RuntimeException()).when(mockedList).clear();

 

Parameters match the simulation method call:

  1. // anyInt () matches any int parameter, which means that parameter to any value, its return value are element  
  2. when(mockedList.get(anyInt())).thenReturn("element");   
  3.   
  4. // print at this time is the element   
  5. System.out.println(mockedList.get(999)); 

Simulation Method Invocations:

  1. // add a call   
  2. mockedList.add("once");   
  3.   
  4. // verify the effect of the following two writing as to verify that all the add method is called once  
  5. verify(mockedList).add("once");   
  6. verify(mockedList, times(1)).add("once");  
 May also be replace time (int i) minimum and maximum values ​​to verify the called number by atLeast (int i) and atMost (int i).

End - it is very simple.
This article is basically a reprint, next article will describe how to use Mockito in code, Finally, most of the functionality of mockito.

 

Original Address: https://blog.csdn.net/OnlyQi/article/details/6396646

Guess you like

Origin www.cnblogs.com/jpfss/p/10972462.html