Skip to content

Commit 3a04c3a

Browse files
TSobalazhendrikse
authored andcommitted
BAEL-832 currently executed method. (eugenp#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 66471cf. * Revert of evaluation task "injecting beans" This reverts commit d2ac201. * [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.
1 parent 6e84c89 commit 3a04c3a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.java.currentmethod;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* The class presents various ways of finding the name of currently executed method.
9+
*/
10+
public class CurrentlyExecutedMethodFinderTest {
11+
12+
@Test
13+
public void givenCurrentThread_whenGetStackTrace_thenFindMethod() {
14+
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
15+
assertEquals("givenCurrentThread_whenGetStackTrace_thenFindMethod", stackTrace[1].getMethodName());
16+
}
17+
18+
@Test
19+
public void givenException_whenGetStackTrace_thenFindMethod() {
20+
String methodName = new Exception().getStackTrace()[0].getMethodName();
21+
assertEquals("givenException_whenGetStackTrace_thenFindMethod", methodName);
22+
}
23+
24+
@Test
25+
public void givenThrowable_whenGetStacktrace_thenFindMethod() {
26+
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
27+
assertEquals("givenThrowable_whenGetStacktrace_thenFindMethod", stackTrace[0].getMethodName());
28+
}
29+
30+
@Test
31+
public void givenObject_whenGetEnclosingMethod_thenFindMethod() {
32+
String methodName = new Object() {}.getClass().getEnclosingMethod().getName();
33+
assertEquals("givenObject_whenGetEnclosingMethod_thenFindMethod", methodName);
34+
}
35+
36+
@Test
37+
public void givenLocal_whenGetEnclosingMethod_thenFindMethod() {
38+
class Local {};
39+
String methodName = Local.class.getEnclosingMethod().getName();
40+
assertEquals("givenLocal_whenGetEnclosingMethod_thenFindMethod", methodName);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)