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