Mockito Getting Started learning

Source: Internet
Author: User

Need to import
import static org.mockito.mockito.*; Import static Org.junit.assert.*;import Java.util.iterator;import org.junit.Test;
Mocks can simulate a variety of objects, replacing real objects to make the desired response.
Ways to use Mockito:
1. Simulate objects using mock ().
An object that simulates linklist
LinkedList mockdedlist = mock (linkedlist.class);
This time, with the Get method, returns null because the return value of the method call has not been simulated.
System.out.printlin (Mockedlist.get (99));
2, specifies the return value when the mock object is called.
A, the impersonation, the return value of the method call.
When the first element is fetched, the string is returned. Returns a fixed value to a specific method call, called a stub in the official statement.
(Stub, to block a client from invoking objects on a remote host, you must provide some way to emulate the local object, which is known as a stub stub)
When (Mockedlist.get (0). Thenreturn ("first"));
At this point, print out first
System.out.println (mockedlist.get (0));
B, impersonation, method call throw exception
Throws a runtimeexception when the second element is fetched
When (Mockedlist.get (1)). Thenthrow (new runtimeexception);
Throw RuntimeException exception at this time
System.out.println (Mockedlist.get (1));

Methods that do not return a value type can also simulate exception throws:
Dothrow (New RuntimeException ()). When (Mockedlist). Clear ();
c. parameter matching when simulating call method
Anyint () matches any int parameter, which means that the parameter is any value and returns the element
When (Mockedlist.get (Anyint ())). Thenreturn ("element");
Printing is element at this time
System.out.println (Mockedlist.get (99));
D. Number of analog method calls
Call Add once
Mockedlist.add ("Once");
Verify that the Add method has been called once and that the two have the same effect
Verify (Mockedlist) Add ("Once");
Verify (Mockedlist,times (1)). Add ("Once");
You can also verify the minimum and maximum number of times that are called by atleast (int i) and tamost (int i)
3. Verify that the class being tested works correctly, using verify ().

By default, Mockito returns the corresponding default value for all places with return values and no stubs.
For built-in types, default values are returned, such as int returns 0, Boolean returns FALSE, and NULL for other type.
The mock object overwrites the entire mock object, so no stub method can return only the default value.
Repeat the stub two times, whichever is the second, the following will return "second"
When (Mockedlist.get (0)). Thenreturn ("first");
When (Mockedlist.get (0)). Thenreturn ("second");
The following form indicates that the first call returns "primary", the second call returns "second" and can write n multiple
When (Mockedlist.get (0)). Thenreturn ("First"). Thenreturn ("second");
If the number of actual calls exceeds the number of stubs, the value of the last stub is returned, as in the previous example, the third call to get (0) returns "second"

The validation method has been called for a specific number of times
Verify that the Add method was called twice
Verify (Mockedlist,times (2)). Add ("2");
Verify that the Add method is called at least once
Verify (Mockedlist.atleastonce ()). Add ("2");
Verify that the Add method is called at least two times
Verify (Mockedlist,atleast (2)). Add ("2");
Verify that the Add method is called 5 times maximum
Verify (Mockedlist,atmost (5)). Add ("2");
Verify that the Add method has never been called
Vreify (Mockedlist,never ()). Add ("2"), find redundant calls, use never ();



Mockito Getting Started learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.