From 648e4aa9ccaea26b931eb3165d19b921bf79dd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Mon, 16 Nov 2020 14:16:58 +0800 Subject: [PATCH] print code location in warning message --- .../com/alibaba/testable/core/tool/InvokeVerifier.java | 7 ++++--- .../com/alibaba/testable/core/util/TestableUtil.java | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java index 508ccb0..186f755 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java @@ -2,6 +2,7 @@ package com.alibaba.testable.core.tool; import com.alibaba.testable.core.error.VerifyFailedError; import com.alibaba.testable.core.model.Verification; +import com.alibaba.testable.core.util.TestableUtil; import java.security.InvalidParameterException; import java.util.List; @@ -137,7 +138,7 @@ public class InvokeVerifier { */ public InvokeVerifier withTimes(int expectedCount) { if (expectedCount != records.size()) { - throw new VerifyFailedError("times: " + records.size(), "times: " + expectedCount); + throw new VerifyFailedError("times: " + expectedCount, "times: " + records.size()); } lastVerification = null; return this; @@ -150,8 +151,8 @@ public class InvokeVerifier { public InvokeVerifier times(int count) { if (lastVerification == null) { // when used independently, equals to `withTimes()` - System.out.println("Warning: using \"times()\" check without \"with()\" or \"withInOrder()\" method " + - "is not recommended, please use \"withTimes()\" instead."); + System.out.println("Warning: [" + TestableUtil.getPreviousStackLocation() + "] using \"times()\" method " + + "without \"with()\" or \"withInOrder()\" is not recommended, please use \"withTimes()\" instead."); return withTimes(count); } if (count < 2) { diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java index 4142451..5058fd4 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java @@ -50,6 +50,15 @@ public class TestableUtil { return ""; } + /** + * Get file name and line number of where current method was called + */ + public static String getPreviousStackLocation() { + // 0 - Thread.getStackTrace(), 1 - this method, 2 - code call this method, 3 - code call the caller method + StackTraceElement stack = getMainThread().getStackTrace()[3]; + return stack.getFileName() + ":" + stack.getLineNumber(); + } + private static String findLastMethodFromSourceClass(String sourceClassName, StackTraceElement[] stack) { for (StackTraceElement element : stack) { if (element.getClassName().equals(sourceClassName)) {