|
10 | 10 | import java.io.ByteArrayOutputStream; |
11 | 11 | import java.io.InputStream; |
12 | 12 | import java.io.PrintStream; |
| 13 | +import java.lang.reflect.Constructor; |
13 | 14 | import java.lang.reflect.Field; |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.lang.reflect.Parameter; |
14 | 17 | import java.util.*; |
15 | 18 |
|
16 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; |
17 | 20 | import static org.junit.jupiter.api.Assertions.assertTrue; |
18 | | -import static org.junit.platform.commons.util.ReflectionUtils.isPrivate; |
19 | | -import static org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass; |
| 21 | +import static org.junit.platform.commons.util.ReflectionUtils.*; |
20 | 22 |
|
21 | 23 | public class Module04_Test { |
22 | 24 | private final String classToFind = "com.h2.MortgageCalculator"; |
@@ -86,10 +88,45 @@ public void m04_02_testExistenceOfPrivateFields() { |
86 | 88 | actualFieldsToClass.put(field.getName(), field.getType()); |
87 | 89 | } |
88 | 90 |
|
89 | | - expectedFieldsToClass.entrySet() |
90 | | - .forEach(e -> { |
91 | | - assertEquals(e.getValue(), actualFieldsToClass.get(e.getKey()), e.getKey() + " must be of type " + e.getValue()); |
92 | | - }); |
| 91 | + expectedFieldsToClass.forEach((key, value) -> assertEquals(value, actualFieldsToClass.get(key), key + " must be of type " + value)); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void m4_03_testMortgageConstructorAndCorrectness() throws IllegalAccessException, InvocationTargetException, InstantiationException { |
| 97 | + final Optional<Class<?>> maybeMortgageCalculator = getMortgageClass(); |
| 98 | + assertTrue(maybeMortgageCalculator.isPresent(), classToFind + " must exist"); |
| 99 | + final Class<?> mortgageCalculator = maybeMortgageCalculator.get(); |
| 100 | + final Constructor<?>[] constructors = mortgageCalculator.getDeclaredConstructors(); |
| 101 | + |
| 102 | + assertEquals(1, constructors.length, classToFind + " should have 1 constructor"); |
| 103 | + |
| 104 | + final Constructor<?> constructor = constructors[0]; |
| 105 | + assertTrue(isPublic(constructor), "constructor must be declared 'public'"); |
| 106 | + assertEquals(3, constructor.getParameterCount(), "Constructor should have 3 parameters"); |
| 107 | + |
| 108 | + Parameter[] parameters = constructor.getParameters(); |
| 109 | + assertEquals(long.class, parameters[0].getType(), "Constructor's first parameter should be of type 'long'"); |
| 110 | + assertEquals(int.class, parameters[1].getType(), "Constructor's second parameter should be of type 'int'"); |
| 111 | + assertEquals(float.class, parameters[2].getType(), "Constructor's third parameter should be of type 'float'"); |
| 112 | + |
| 113 | + final long loanAmount = 100L; |
| 114 | + final int termInYears = 20; |
| 115 | + final float annualRate = 2.65f; |
| 116 | + |
| 117 | + Object instance = constructor.newInstance(loanAmount, termInYears, annualRate); |
| 118 | + |
| 119 | + final Field[] fields = mortgageCalculator.getDeclaredFields(); |
| 120 | + for (Field field : fields) { |
| 121 | + field.setAccessible(true); |
| 122 | + if (field.getName().equals("loanAmount")) { |
| 123 | + assertEquals(loanAmount, (long) field.get(instance), "loanAmount should have value of " + loanAmount); |
| 124 | + } else if (field.getName().equals("termInYears")) { |
| 125 | + assertEquals(termInYears, (int) field.get(instance), "termInYears should have value of " + termInYears); |
| 126 | + } else if (field.getName().equals("annualRate")) { |
| 127 | + assertEquals(annualRate, (float) field.get(instance), "annualRate should have value of " + annualRate); |
| 128 | + } |
| 129 | + } |
93 | 130 |
|
94 | 131 | } |
95 | 132 | } |
0 commit comments