From 4c79e53dcc2df4cac57de11d9641e9a5829120c4 Mon Sep 17 00:00:00 2001 From: hhimanshu Date: Sat, 23 May 2020 13:32:13 -0700 Subject: [PATCH] Added m5_08_testFinanceMainMethodExists, m5_09_testFinanceMainMethodWorksCorrectly --- src/test/java/com/h2/Module05_Test.java | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/test/java/com/h2/Module05_Test.java b/src/test/java/com/h2/Module05_Test.java index 970bd211c..2375bccc2 100644 --- a/src/test/java/com/h2/Module05_Test.java +++ b/src/test/java/com/h2/Module05_Test.java @@ -270,4 +270,70 @@ public void m5_07_testExecuteCommandExistenceForCorrectness() { assertEquals("No available rates for term: " + loanTermInYears + " years", consoleOutputs.get(4)); } } + + @Test + public void m5_08_testFinanceMainMethodExists() { + final String methodName = "main"; + + final Optional> maybeClass = getFinanceClass(); + assertTrue(maybeClass.isPresent(), classToFind + " must exist"); + final Class aClass = maybeClass.get(); + + final Method[] methods = aClass.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 method = filteredMethod.get(0); + assertTrue(isPublic(method), methodName + " must be declared as 'public'"); + assertTrue(isStatic(method), methodName + " must be declared as 'static'"); + assertEquals(void.class, method.getReturnType(), methodName + " method must return a value of type 'void'"); + + final Class[] parameterTypes = method.getParameterTypes(); + assertEquals(1, parameterTypes.length, methodName + " must accept 1 parameter."); + assertEquals(String[].class, parameterTypes[0], methodName + " must accept only 1 parameter of type 'String[]'"); + } + @Test + public void m5_09_testFinanceMainMethodWorksCorrectly() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + final String methodName = "main"; + + final Optional> maybeClass = getFinanceClass(); + assertTrue(maybeClass.isPresent(), classToFind + " must exist"); + final Class aClass = maybeClass.get(); + + final Method[] methods = aClass.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 + "'"); + + + + Method method = aClass.getMethod("main", String[].class); + { + String command = "LaunchRocketToMoon"; + method.invoke(null, (Object) new String[]{command}); + assertEquals(command + ": command not found" + "\n", testOut.toString()); + } + { + setUpOutput(); + final String loanAmount = "264000"; + final String termInYears = "30"; + method.invoke(null, (Object) new String[]{"mortgageCalculator", loanAmount, termInYears}); + assertEquals("usage: mortgageCalculator " + "\n", testOut.toString()); + } + { + setUpOutput(); + final String loanAmount = "264000"; + final String termInYears = "30"; + final String annualRate = "3.74"; + + method.invoke(null, (Object) new String[]{"mortgageCalculator", loanAmount, termInYears, annualRate}); + //invokeMethod(method, null, "mortgageCalculator", new String[]{loanAmount, termInYears, annualRate}); + + List consoleOutputs = Arrays.asList(testOut.toString().split("\n")); + + assertEquals(2, consoleOutputs.size(), "For case MORTGAGE_CALCULATOR, " + methodName + " should print 2 statements on the console. One for 'Finding your monthly payment ...' and another one should be the output from the MortgageCalculator"); + assertEquals("Finding your monthly payment ...", consoleOutputs.get(0)); + assertEquals("monthlyPayment: 1221.14", consoleOutputs.get(1)); + } + } }