Org.mockito.Matchers
Everybody do
Google search daily (you reached to my blog via some search engine only), what
you typed ‘mockito matchers’ in search and it rendered you my blog at 10th
-12th -30th place, or might be somewhere.
What it did,
it matched the string provided by you and showed you any() website which is having reference to the mockito matchers word.
This is exactly what
our Mockito Matchers class do:
1.
It will allow to cutomisely mock the
parameter supplied to java methods:
2. Method paramater could be anyString(),
anyInt(), anyDouble(), anyFloat(), anyObject() or eq() to something, matching to some pattern or contains some part of given string etc..
The mockito
matchers, gives you the fexibility to test your code methods, instead of a particular value, just anyXXX().
e.g.
To mock Employee class method
printSummary (name(String),total_marks(Float),Address(String))
and test for anyName, any marks and any aqddress print “Sushil from Bangalore got 79.5 out of 150”
we can use matchers like this:
we can use matchers like this:
When(mockedEmployee.printSummary(anyString(),
anyFloat(), anyString())).thenReturn(“Sushil from Bangalore got 79.5 out of
150”);
Here is the list of all methods
provided by matcher.
do remember all methods are static you just have to statically
import org.mockito.Matcher class at the top, and then just call these methods.
1.
any() – matches anything, null as
well.
2.
any(some.class) – matches the
class instance of some and null as well.
3.
anyBoolean()
4.
anyChar()
5.
anyInt()
6.
anyFloat()
7.
anyByte()
8.
anyCollection(),
anyCollectionof(Class<T> clazz) : matches any instance of
Collection.class.
9.
anyList(),
anyListof(Class<T> clazz): matches any instance of List.class
10. anyMap(), anyMapof(Class<T>
clazz) : matches any instance of Map.class.
11. anySet(),anySetOf():
12. anyVararg() – do you want to test
any number of arguments, take anyVarag() then.
Exp:
when(mockEmployee().getSummary(anyVararg())).thenReturn (“Sushil”)
it will return “sushil”, when
Employee.getSummary() will called with any number of parameters.
13. contains(String substring) :
check for the method parameter containing that substring.
verify(mockEmployee).getSummary(contains("Sushil"),anyint(),anyString());
14. endsWith(String suffix) : check
for the method parameter ending with string.
verify(mockEmployee).getSummary(endsWith("sushil"),anyint(),anyString());
15. eq(byte|char|float|String|int|double|T|etc….
value): it will check for the method parameter, which is equal to given value:
verify(mockEmployee).getSummary(eq("sushil"),eq(10),anyString());
16. isA(Class<T> clazz) :
ensure the method parameter is child of clazz or clazz itself.
17. isNotNull(), isNull()- self
explainetory
18. matches(regex) – ensure that the
method parameter matching the regex value.
In our example, if we want to test
only int values below 100, we can created a regex like (0-9)*10 for int value.
When((mockEmployee).getSummary(eq(“Sushil”),matches((0-9)*10)),
endswith(“Bangalore”))).thenRetuern(“The worst student”)
19. same ( T value) : object value
which is same as the value passed.
20. startsWith(String prefix) :
opposite of endsWith(String suffix)
No comments:
Post a Comment