@@ -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 CurrentlyExecutedMethodFinderUnitTest {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -200,7 +200,7 @@ public class ReflectionUnitTest {
|
||||
@Test
|
||||
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Bird bird = (Bird) birdClass.newInstance();
|
||||
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||
final Field field = birdClass.getDeclaredField("walks");
|
||||
field.setAccessible(true);
|
||||
|
||||
@@ -266,7 +266,7 @@ public class ReflectionUnitTest {
|
||||
@Test
|
||||
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Bird bird = (Bird) birdClass.newInstance();
|
||||
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||
final Method walksMethod = birdClass.getDeclaredMethod("walks");
|
||||
final boolean walks = (boolean) walksMethod.invoke(bird);
|
||||
|
||||
Reference in New Issue
Block a user