Next: @Mock annotations
public class Articlemanagertest {
@Mock private articlecalculator calculator;
@Mock private articledatabase database;
@Mock private Userprovider Userprovider;
Private Articlemanager manager;
The important thing is that you need to call
Mockitoannotations.initmocks (TestClass) in some base classes or test cases;
. Chained style of writing
When (Mock.somemethod ("some arg"))
. Thenthrow (New RuntimeException ())
. Thenreturn ("foo");
First Call:throws runtime exception:
Mock.somemethod ("some arg");
Second call:prints "foo"
System.out.println (Mock.somemethod ("some arg"));
Any consecutive call:prints ' foo ' as well (last stubbing wins).
System.out.println (Mock.somemethod ("some arg"));
Of course you can also write when
(Mock.somemethod ("some arg"))
. Thenreturn ("One", "the" "the", "the" the "three");
Using Thenreturn,thenthrow we have enough coverage for some simple tests, and if you need some callback tests, the following sample
When (Mock.somemethod (AnyString ())). Thenanswer (New Answer () {
Object Answer (Invocationonmock invocation) {
object[] args = Invocation.getarguments ();
Object mock = Invocation.getmock ();
Return "called with arguments:" + args;
}
);
The following prints "called with Arguments:foo"
System.out.println (Mock.somemethod ("foo"));
A different when (object) is required for an empty return method because it does not like the empty method in the use case when compiling
Dothrow (New RuntimeException ()). When (Mockedlist). Clear ();
Following throws RuntimeException:
mockedlist.clear ();
Doreturn (Object)
Dothrow (Throwable ...)
Dothrow (Class)
doanswer (Answer)
doNothing ()
Docallrealmethod ()
Method of stub empty
The method of stub in the Spy object (see below) Spy object
List List = new LinkedList ();
List spy = Spy (list);
Optionally, you can stub out some methods: when
(Spy.size ()). Thenreturn (+);
Using the Spy calls *real* methods
Spy.add ("one");
Spy.add ("both");
Prints "One"-the first element of a list
System.out.println (spy.get (0));
Size () method was stubbed-100 is printed
System.out.println (Spy.size ());
Optionally, you can verify
verify (Spy). Add ("one");
Verify (Spy). Add ("both");
Important note, sometimes we use the When (object) in stubbing spies we need doreturn| answer| Some member methods in the Throw () stubbing, such as
List List = new LinkedList ();
List spy = Spy (list);
Impossible:real method is called so Spy.get (0) throws Indexoutofboundsexception (the list was yet empty) when
(SPY.G ET (0)). Thenreturn ("foo");
You have the To use Doreturn () for stubbing
doreturn ("foo"). When (Spy). Get (0);
You can create a specified return value for a simulation with the specified policy (since 1.7)
Foo mock = mock (Foo.class, mockito.returns_smart_nulls);
Foo mocktwo = mock (Foo.class, New Yourownanswer ());
public static final Answer returns_smart_nulls true partial mock (since 1.8)
You can create a partial mock with Spy () method:
List List = Spy (new LinkedList ());
You can enable the partial mock capabilities selectively on mocks:
Foo mock = mock (foo.class);
Being sure the real implementation is ' safe '.
If Real implementation throws exceptions or depends on specific state of the object then you ' re in trouble.
When (Mock.somemethod ()). Thencallrealmethod ();
The reset of the mock
List mock = mock (list.class);
When (Mock.size ()). Thenreturn (ten);
Mock.add (1);
Reset (mock);
At this point the mock forgot any interactions & stubbing
Don't hurt yourself, reset () there's a bit of bad code in a test method, and you might be able to test more behavior-driven development aliases (since 1.8)
The test style of BDD, the current subbing API when usage does not given when and then style syntax is clear.
Import static org.mockito.bddmockito.*;
Seller Seller = mock (seller.class);
Shop shop = new shop (seller);
public void Shouldbuybread () throws Exception {
//given
given (Seller.askforbread ()). Willreturn (New Bread ());
When
Goods Goods = Shop.buybread ();
Then
assertthat (Goods, containbread ());
}
When the shop object calls Buybread, the method calls the Askforbread () method inside the seller, returns the new Bread (), and finally Assertthat compares two objects
Serialized mock (since 1.8.1)
Added a serialized mock to the new feature,
List Serializablemock = mock (List.class, Withsettings (). Serializable ());
A serialized partial mock with a BBD style
list<object> List = new arraylist<object> (); List<object> spy = Mock (Arraylist.class, Withsettings (). Spiedinstance (list). Defa Ultanswer (Calls_real_methods). Serializable ());