From 671784559ac2abdbb272180b527062597bc8abcc Mon Sep 17 00:00:00 2001 From: hhimanshu Date: Thu, 21 May 2020 17:27:17 -0700 Subject: [PATCH] Added m4_09_testCalculateMonthlyPaymentCorrectness --- src/test/java/com/h2/Module04_Test.java | 52 +++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/h2/Module04_Test.java b/src/test/java/com/h2/Module04_Test.java index 495e7340e..1d974d2e0 100644 --- a/src/test/java/com/h2/Module04_Test.java +++ b/src/test/java/com/h2/Module04_Test.java @@ -13,8 +13,7 @@ import java.util.*; import java.util.stream.Collectors; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.junit.platform.commons.util.ReflectionUtils.*; public class Module04_Test { @@ -223,7 +222,7 @@ public void m4_07_testMonthlyInterestRateCorrectness() throws IllegalAccessExcep final long loanAmount = 100L; final int termInYears = 20; - final float annualRate = 2.65f; + final float annualRate = 3.74f; Object instance = constructor.newInstance(loanAmount, termInYears, annualRate); @@ -235,7 +234,7 @@ public void m4_07_testMonthlyInterestRateCorrectness() throws IllegalAccessExcep assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'"); Method method = filteredMethod.get(0); final float result = (float) invokeMethod(method, instance); - float expected = annualRate / 12; + float expected = (annualRate / 100) / 12; assertEquals(expected, result, methodName + " should return " + expected + " as monthly interest rate for a annualRate of " + annualRate); } @@ -256,4 +255,49 @@ public void m4_08_testExistenceOfCalculateMonthlyPayment() { assertTrue(isPublic(method), methodName + " must be declared as 'public'"); assertEquals(void.class, method.getReturnType(), methodName + " method must return a value of type 'void'"); } + + @Test + public void m4_09_testCalculateMonthlyPaymentCorrectness() throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { + final Optional> maybeMortgageCalculator = getMortgageClass(); + assertTrue(maybeMortgageCalculator.isPresent(), classToFind + " must exist"); + final Class mortgageCalculator = maybeMortgageCalculator.get(); + final Constructor[] constructors = mortgageCalculator.getDeclaredConstructors(); + + assertEquals(1, constructors.length, classToFind + " should have 1 constructor"); + + final Constructor constructor = constructors[0]; + assertTrue(isPublic(constructor), "constructor must be declared 'public'"); + assertEquals(3, constructor.getParameterCount(), "Constructor should have 3 parameters"); + + Parameter[] parameters = constructor.getParameters(); + assertEquals(long.class, parameters[0].getType(), "Constructor's first parameter should be of type 'long'"); + assertEquals(int.class, parameters[1].getType(), "Constructor's second parameter should be of type 'int'"); + assertEquals(float.class, parameters[2].getType(), "Constructor's third parameter should be of type 'float'"); + + final long loanAmount = 264000; + final int termInYears = 30; + final float annualRate = 3.74f; + + Object instance = constructor.newInstance(loanAmount, termInYears, annualRate); + + final String methodName = "calculateMonthlyPayment"; + final List filteredMethod = Arrays.stream(mortgageCalculator.getDeclaredMethods()) + .filter(method -> method.getName().equals(methodName)) + .collect(Collectors.toList()); + + assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'"); + Method method = filteredMethod.get(0); + invokeMethod(method, instance); + double expected = 1221.1400903847025; + + String fieldName = "monthlyPayment"; + Field field = mortgageCalculator.getDeclaredField(fieldName); + field.setAccessible(true); + + double actual = field.getDouble(instance); + assertNotEquals(0.0, actual, fieldName + " should not be 0.0"); + + boolean areAlmostSame = Math.abs(expected - actual) < 0.001; + assertTrue(areAlmostSame, fieldName + " should be " + expected + " (or with a margin of +0.001), but was " + actual); + } }