diff --git a/src/test/java/com/h2/Module04_Test.java b/src/test/java/com/h2/Module04_Test.java index 0ad1deefc..61e0181b6 100644 --- a/src/test/java/com/h2/Module04_Test.java +++ b/src/test/java/com/h2/Module04_Test.java @@ -17,7 +17,7 @@ import static org.junit.platform.commons.util.ReflectionUtils.*; public class Module04_Test { - private final String classToFind = "com.h2.MortgageCalculator"; + private static final String classToFind = "com.h2.MortgageCalculator"; private final InputStream systemIn = System.in; private final PrintStream systemOut = System.out; @@ -50,7 +50,7 @@ private static Optional> getClass(final String className) { return aClass.toOptional(); } - public Optional> getMortgageClass() { + public static Optional> getMortgageClass() { return getClass(classToFind); } diff --git a/src/test/java/com/h2/Module06_Test.java b/src/test/java/com/h2/Module06_Test.java index e02f67516..7dfc08883 100644 --- a/src/test/java/com/h2/Module06_Test.java +++ b/src/test/java/com/h2/Module06_Test.java @@ -5,7 +5,10 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.*; import static org.junit.platform.commons.util.ReflectionUtils.*; @@ -131,4 +134,43 @@ public void m6_05_testGetFloatValueExistenceAndCorrectness() throws IllegalAcces fail("Can't find a method with name " + methodName + " in class " + classToFind + " with 1 parameter of type 'String'"); } } + + @Test + public void m6_06_testMainMethodPrintsCorrectMortgageAmount() throws NoSuchMethodException { + final String methodName = "main"; + + final Optional> maybeMortgageCalculator = Module04_Test.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 String loanAmount = "264000"; + final String termInYears = "30"; + final String annualRate = "3.74"; + String input = "hello"; + + Method method = mortgageCalculator.getMethod("main", String[].class); + { + final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, (Object) new String[]{input, termInYears, annualRate})); + Throwable targetException = exception.getTargetException(); + assertEquals(IllegalArgumentException.class, targetException.getClass(), methodName + " should have thrown an instance of 'IllegalArgumentException'"); + assertEquals(input + " cannot be converted into a 'long' value. Exiting program.", targetException.getMessage()); + } + { + final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, (Object) new String[]{loanAmount, input, annualRate})); + Throwable targetException = exception.getTargetException(); + assertEquals(IllegalArgumentException.class, targetException.getClass(), methodName + " should have thrown an instance of 'IllegalArgumentException'"); + assertEquals(input + " cannot be converted into a 'int' value. Exiting program.", targetException.getMessage()); + } + { + final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, (Object) new String[]{loanAmount, termInYears, input})); + Throwable targetException = exception.getTargetException(); + assertEquals(IllegalArgumentException.class, targetException.getClass(), methodName + " should have thrown an instance of 'IllegalArgumentException'"); + assertEquals(input + " cannot be converted into a 'float' value. Exiting program.", targetException.getMessage()); + } + } }