Merge branch 'master' into JAVA-7244-Review_log_statements_for_projects

This commit is contained in:
Loredana Crusoveanu
2021-12-09 10:22:41 +02:00
committed by GitHub
1079 changed files with 12491 additions and 4924 deletions

View File

@@ -0,0 +1,12 @@
package com.baeldung.reflection.access.staticmethods;
public class GreetingAndBye {
public static String greeting(String name) {
return String.format("Hey %s, nice to meet you!", name);
}
private static String goodBye(String name) {
return String.format("Bye %s, see you next time.", name);
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.reflection.check.abstractclass;
import com.baeldung.reflection.access.staticmethods.GreetingAndBye;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class GreetingAndByeUnitTest {
@Test
void givenPublicStaticMethod_whenCallWithReflection_thenReturnExpectedResult() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<GreetingAndBye> clazz = GreetingAndBye.class;
Method method = clazz.getMethod("greeting", String.class);
Object result = method.invoke(null, "Eric");
Assertions.assertEquals("Hey Eric, nice to meet you!", result);
}
@Test
void givenPrivateStaticMethod_whenCallWithReflection_thenReturnExpectedResult() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<GreetingAndBye> clazz = GreetingAndBye.class;
Method method = clazz.getDeclaredMethod("goodBye", String.class);
method.setAccessible(true);
Object result = method.invoke(null, "Eric");
Assertions.assertEquals("Bye Eric, see you next time.", result);
}
}