From a0defac683d51f50df4c38ddd8421cd087b273b9 Mon Sep 17 00:00:00 2001 From: hhimanshu Date: Thu, 21 May 2020 17:34:39 -0700 Subject: [PATCH] Added m3_10_testMainMethodExists --- src/test/java/com/h2/Module04_Test.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/com/h2/Module04_Test.java b/src/test/java/com/h2/Module04_Test.java index 1d974d2e0..c3c909818 100644 --- a/src/test/java/com/h2/Module04_Test.java +++ b/src/test/java/com/h2/Module04_Test.java @@ -300,4 +300,27 @@ public void m4_09_testCalculateMonthlyPaymentCorrectness() throws IllegalAccessE boolean areAlmostSame = Math.abs(expected - actual) < 0.001; assertTrue(areAlmostSame, fieldName + " should be " + expected + " (or with a margin of +0.001), but was " + actual); } + + @Test + public void m3_10_testMainMethodExists() { + final String methodName = "main"; + + 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 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[]'"); + } }