diff --git a/core-java-modules/core-java-reflection-2/pom.xml b/core-java-modules/core-java-reflection-2/pom.xml index 2cb6d722a8..ee3eeee734 100644 --- a/core-java-modules/core-java-reflection-2/pom.xml +++ b/core-java-modules/core-java-reflection-2/pom.xml @@ -47,6 +47,25 @@ -parameters + + org.jacoco + jacoco-maven-plugin + 0.8.8 + + + + prepare-agent + + + + report + prepare-package + + report + + + + diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/privatemethods/Utils.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/privatemethods/Utils.java new file mode 100644 index 0000000000..f3ff0b5cf5 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/privatemethods/Utils.java @@ -0,0 +1,19 @@ +package com.baeldung.reflection.privatemethods; + +public class Utils { + + public static Integer validateAndDouble(Integer input) { + if (input == null) { + throw new IllegalArgumentException("input should not be null"); + } + return doubleInteger(input); + } + + private static Integer doubleInteger(Integer input) { + if (input == null) { + return null; + } + return 2 * input; + } + +} diff --git a/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/privatemethods/UtilsUnitTest.java b/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/privatemethods/UtilsUnitTest.java new file mode 100644 index 0000000000..59fa98c7d4 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/privatemethods/UtilsUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.reflection.privatemethods; + +import static com.baeldung.reflection.privatemethods.Utils.validateAndDouble; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.jupiter.api.Test; + +public class UtilsUnitTest { + + // Let's start with the tests of the public API + @Test + void givenNull_WhenValidateAndDouble_ThenThrows() { + assertThrows(IllegalArgumentException.class, () -> validateAndDouble(null)); + } + + @Test + void givenANonNullInteger_WhenValidateAndDouble_ThenDoublesIt() { + assertEquals(4, validateAndDouble(2)); + } + + // Further on, let's test the private method + @Test + void givenNull_WhenDoubleInteger_ThenNull() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { + assertEquals(null, getDoubleIntegerMethod().invoke(null, new Integer[] { null })); + } + + @Test + void givenANonNullInteger_WhenDoubleInteger_ThenDoubleIt() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + assertEquals(74, getDoubleIntegerMethod().invoke(null, 37)); + } + + private Method getDoubleIntegerMethod() throws NoSuchMethodException { + Method method = Utils.class.getDeclaredMethod("doubleInteger", Integer.class); + method.setAccessible(true); + return method; + } + +}