BAEL-5461 code for the Unit Test Private Methods in Java article (#12240)
Co-authored-by: thibault.faure <thibault.faure@mimacom.com>
This commit is contained in:
parent
220f50f2e2
commit
34af11b770
|
@ -47,6 +47,25 @@
|
|||
<compilerArgument>-parameters</compilerArgument>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.8</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>prepare-agent</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>report</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>report</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue