4.0 KiB
Verify Mock Invocation
In the test, in addition to replacing methods contain external dependencies with mocks, it is often necessary to verify whether the actual parameters of mock invocation are in accordance with expectations.
The verifiers and matchers are provided in TestableMock
to achieve this function. for example:
@Test
public test_case() {
int res = insToTest.methodToTest();
verify("mockMethod").with(123, "abc");
}
This use case will check whether the mock method named mockMethod
has been called when the method under test methodToTest()
is executed, and whether the parameter values received during the call are 123
and "abc"
(Assuming that the mockMethod
method has two parameters).
In addition to this simple verification, TestableMock
currently supports a variety of validators, as well as matchers that can fuzzy match parameter characteristics.
The DemoMatcherTest
test classes in the sample projects java-demo
and kotlin-demo
show the usage of these validators and matchers in detail.
Basic validator
with(Object...args)
→ verify whether the method has been called by the specified parameterswithInOrder(Object... args)
→ verify the specified method is called with specified parameters according to the actual calling orderwithTimes(int expectedCount)
→ verify whether the method has been called the specified number of times, ignoring the check of the calling parameterswithout(Object...args)
→ verification method has never been called with specified parameterstimes(int count)
→ use after thewith()
orwithInOrder()
method to verify that the method has been called the specified number of times with the same conditions
Basic matcher
any()
→ matches any value, includingnull
any(Class<?> clazz)
→ match any value of the specified type or subtypeanyTypeOf(Class<?>... classes)
→ match any type of value in the listanyString()
→ matches any stringanyNumber()
→ matches any number (integer, long, float, double, etc)anyBoolean()
→ matches any Boolean valueanyByte()
→ match any single byte type valueanyChar()
→ matches any single character type valueanyInt()
→ matches any integer valueanyLong()
→ matches any value of long integer typeanyFloat()
→ match any value of float typeanyDouble()
→ match any double-precision float number type valueanyShort()
→ matches any value of short integer typeanyArray()
→ matches any arrayanyArrayOf(Class<?> clazz)
→ matches any array of the specified typeanyList()
→ matches any listanyListOf(Class<?> clazz)
→ matches any list of the specified typeanySet()
→ matches any setanySetOf(Class<?> clazz)
→ matches any set of the specified typeanyMap()
→ match any mapanyMapOf(Class<?> keyClass, Class<?> valueClass)
→ match any map of the specified typeanyCollection()
→ match any containeranyCollectionOf(Class<?> clazz)
→ matches any specified type of containeranyIterable()
→ matches any iteratoranyIterableOf(Class<?> clazz)
→ matches any iterator of the specified typeeq(Object obj)
→ match objects equal to the specified valuerefEq(Object obj)
→ match the specified object (not equal to the value, but the same object)
Null value matcher
isNull()
→ matchnull
notNull()
→ matches any value exceptnull
nullable(Class<?> clazz)
→ matches any value ofnull
or specified type
String matcher
contains(String substring)
→ match a string containing a specific substringmatches(String regex)
→ match strings that match the specified regular expressionendsWith(String suffix)
→ match the string ending with the specified substringstartsWith(String prefix)
→ match the string starting with the specified substring
Universal matcher
any(MatchFunction matcher)
→ match the value that matches the specified expression