Merge pull request #9567 from alimate/BAEL-4275

BAEL-4275: The Stack-Walking API for Currently Executing Method
This commit is contained in:
Eric Martin 2020-07-02 11:54:51 -05:00 committed by GitHub
commit 1538dc248d
1 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.java9.currentmethod;
import org.junit.Test;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class CurrentExecutingMethodUnitTest {
@Test
public void givenJava9_whenWalkingTheStack_thenFindMethod() {
StackWalker walker = StackWalker.getInstance();
Optional<String> methodName = walker.walk(frames -> frames
.findFirst()
.map(StackWalker.StackFrame::getMethodName)
);
assertTrue(methodName.isPresent());
assertEquals("givenJava9_whenWalkingTheStack_thenFindMethod", methodName.get());
}
}