From 7108a1fc49df0a3ab1d1f30b23a6059d59489482 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Mon, 5 Feb 2018 14:57:33 +0100 Subject: [PATCH] Optimize ApplicationHome.isUnitTest() Closes gh-11910 --- .../java/org/springframework/boot/system/ApplicationHome.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationHome.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationHome.java index 0e70dee354..05e3e5ddea 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationHome.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationHome.java @@ -105,7 +105,9 @@ public class ApplicationHome { private boolean isUnitTest() { try { - for (StackTraceElement element : Thread.currentThread().getStackTrace()) { + StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + for (int i = stackTrace.length - 1; i >= 0; i--) { + StackTraceElement element = stackTrace[i]; if (element.getClassName().startsWith("org.junit.")) { return true; }