handle inner class correctly

This commit is contained in:
金戟 2020-07-26 21:16:46 +08:00
parent 9e1c1b8c3b
commit 2f6c2105f6

View File

@ -18,8 +18,12 @@ public class TestableUtil {
}
public static String currentTestCaseName(Object testClassRef) {
return currentTestCaseName(testClassRef.getClass());
}
public static String currentTestCaseName(Class testClass) {
StackTraceElement[] stack = getMainThread().getStackTrace();
String testClassName = testClassRef.getClass().getName();
String testClassName = getRealClassName(testClass);
for (int i = stack.length - 1; i >= 0; i--) {
if (stack[i].getClassName().equals(testClassName)) {
return stack[i].getMethodName();
@ -28,6 +32,12 @@ public class TestableUtil {
return "";
}
private static String getRealClassName(Class testClass) {
String className = testClass.getName();
int posOfInnerClass = className.lastIndexOf('$');
return posOfInnerClass > 0 ? className.substring(0, posOfInnerClass) : className;
}
private static Thread getMainThread() {
for (Thread t : Thread.getAllStackTraces().keySet()) {
if (t.getId() == 1L) {