diff --git a/src/test/java/com/h2/Module04_Test.java b/src/test/java/com/h2/Module04_Test.java index 1320b1b27..fead18b1e 100644 --- a/src/test/java/com/h2/Module04_Test.java +++ b/src/test/java/com/h2/Module04_Test.java @@ -10,11 +10,9 @@ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Parameter; +import java.lang.reflect.*; import java.util.*; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -133,4 +131,22 @@ public void m4_03_testMortgageConstructorAndCorrectness() throws IllegalAccessEx } } + + @Test + public void m4_03_testExistenceOfNumberOfPayments() { + final String methodName = "getNumberOfPayments"; + + final Optional> maybeMortgageCalculator = getMortgageClass(); + assertTrue(maybeMortgageCalculator.isPresent(), classToFind + " must exist"); + final Class mortgageCalculator = maybeMortgageCalculator.get(); + + final Method[] methods = mortgageCalculator.getDeclaredMethods(); + final List filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList()); + + assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'"); + + final Method sumOfCredits = filteredMethod.get(0); + assertTrue(isPrivate(sumOfCredits), methodName + " must be declared as 'private'"); + assertEquals(int.class, sumOfCredits.getReturnType(), methodName + " method must return a value of type 'int'"); + } }