forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatcherToString.java
43 lines (37 loc) · 1.4 KB
/
MatcherToString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Copyright (c) 2016 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers.text;
import static org.mockito.internal.util.ObjectMethodsGuru.isToStringMethod;
import static org.mockito.internal.util.StringUtil.decamelizeMatcher;
import java.lang.reflect.Method;
import org.mockito.ArgumentMatcher;
/**
* Provides better toString() text for matcher that don't have toString() method declared.
*/
public class MatcherToString {
/**
* Attempts to provide more descriptive toString() for given matcher.
* Searches matcher class hierarchy for toString() method. If it is found it will be used.
* If no toString() is defined for the matcher hierarchy,
* uses decamelized class name instead of default Object.toString().
* This way we can promote meaningful names for custom matchers.
*
* @param matcher
* @return
*/
public static String toString(ArgumentMatcher<?> matcher) {
Class<?> cls = matcher.getClass();
while(cls != Object.class) {
Method[] methods = cls.getDeclaredMethods();
for (Method m : methods) {
if(isToStringMethod(m)) {
return matcher.toString();
}
}
cls = cls.getSuperclass();
}
return decamelizeMatcher(matcher.getClass().getSimpleName());
}
}