From 2f6c2105f6330a7659c45c50a74d545ee0c5b5e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sun, 26 Jul 2020 21:16:46 +0800 Subject: [PATCH] handle inner class correctly --- .../com/alibaba/testable/core/util/TestableUtil.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java b/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java index 43c4b3a..8aa4dd3 100644 --- a/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java +++ b/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java @@ -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) {