also look current thread for test case name

This commit is contained in:
金戟 2020-11-28 22:42:25 +08:00
parent 92f9c27561
commit 35dbc1f497

View File

@ -41,13 +41,11 @@ public class TestableUtil {
* @return method name * @return method name
*/ */
public static String currentTestCaseName(String testClassName) { public static String currentTestCaseName(String testClassName) {
StackTraceElement[] stack = getMainThread().getStackTrace(); String testCaseName = findFirstMethodFromTestClass(testClassName, getMainThread().getStackTrace());
for (int i = stack.length - 1; i >= 0; i--) { if (testCaseName.isEmpty()) {
if (stack[i].getClassName().equals(testClassName)) { return findFirstMethodFromTestClass(testClassName, Thread.currentThread().getStackTrace());
return stack[i].getMethodName();
} }
} return testCaseName;
return "";
} }
/** /**
@ -69,6 +67,15 @@ public class TestableUtil {
return ""; return "";
} }
private static String findFirstMethodFromTestClass(String testClassName, StackTraceElement[] stack) {
for (int i = stack.length - 1; i >= 0; i--) {
if (stack[i].getClassName().equals(testClassName)) {
return stack[i].getMethodName();
}
}
return "";
}
private static String getRealClassName(Class<?> testClass) { private static String getRealClassName(Class<?> testClass) {
String className = testClass.getName(); String className = testClass.getName();
int posOfInnerClass = className.lastIndexOf('$'); int posOfInnerClass = className.lastIndexOf('$');