From 3a04c3adc144efcdf27aa308d2c43fba518f1a97 Mon Sep 17 00:00:00 2001 From: Tomasz Sobala Date: Thu, 15 Jun 2017 23:19:32 +0200 Subject: [PATCH] BAEL-832 currently executed method. (#1982) * injecting beans * XML-based configuration replaced with Java Config. * [BAEL-431] Exploring TestRestTemplate. * Revert of evaluation task "XML-based configuration replaced with Java Config." This reverts commit 66471cf0574c85f8ff514ec4caf5ba44ebba1a74. * Revert of evaluation task "injecting beans" This reverts commit d2ac20185e636245bc0ae0b4ccb952965de88e28. * [BAEL-431] fix to the tests in TestRestTemplateBasicLiveTest. * [BAEL-431] added more meaningful user and password for auth. * [BAEL-820] examples of wait() and sleep() methods. * [BAEL-820] wait() and sleep() examples. * [BAEL-829] number of occurences of a char in a String. * [BAEL-829] printlns changed to assertions. * [BAEL-829] improved names of the tests. * [BAEL-872] map and flatMap difference. * removed duplicated countingChars class. * [BAEL-872] changed Object into List. * [BAEL-872] improved code for map() and flatMap() article. * [BEAL-832] getting the name of current executing method. * clean up. --- .../CurrentlyExecutedMethodFinderTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/java/currentmethod/CurrentlyExecutedMethodFinderTest.java diff --git a/core-java/src/test/java/com/baeldung/java/currentmethod/CurrentlyExecutedMethodFinderTest.java b/core-java/src/test/java/com/baeldung/java/currentmethod/CurrentlyExecutedMethodFinderTest.java new file mode 100644 index 0000000000..9a231a9a3d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/currentmethod/CurrentlyExecutedMethodFinderTest.java @@ -0,0 +1,43 @@ +package com.baeldung.java.currentmethod; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * The class presents various ways of finding the name of currently executed method. + */ +public class CurrentlyExecutedMethodFinderTest { + + @Test + public void givenCurrentThread_whenGetStackTrace_thenFindMethod() { + final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + assertEquals("givenCurrentThread_whenGetStackTrace_thenFindMethod", stackTrace[1].getMethodName()); + } + + @Test + public void givenException_whenGetStackTrace_thenFindMethod() { + String methodName = new Exception().getStackTrace()[0].getMethodName(); + assertEquals("givenException_whenGetStackTrace_thenFindMethod", methodName); + } + + @Test + public void givenThrowable_whenGetStacktrace_thenFindMethod() { + StackTraceElement[] stackTrace = new Throwable().getStackTrace(); + assertEquals("givenThrowable_whenGetStacktrace_thenFindMethod", stackTrace[0].getMethodName()); + } + + @Test + public void givenObject_whenGetEnclosingMethod_thenFindMethod() { + String methodName = new Object() {}.getClass().getEnclosingMethod().getName(); + assertEquals("givenObject_whenGetEnclosingMethod_thenFindMethod", methodName); + } + + @Test + public void givenLocal_whenGetEnclosingMethod_thenFindMethod() { + class Local {}; + String methodName = Local.class.getEnclosingMethod().getName(); + assertEquals("givenLocal_whenGetEnclosingMethod_thenFindMethod", methodName); + } + +}