Skip to content

Commit 1c6252f

Browse files
committed
Added m4_03_testMortgageConstructorAndCorrectness
1 parent 50234da commit 1c6252f

1 file changed

Lines changed: 43 additions & 6 deletions

File tree

src/test/java/com/h2/Module04_Test.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import java.io.ByteArrayOutputStream;
1111
import java.io.InputStream;
1212
import java.io.PrintStream;
13+
import java.lang.reflect.Constructor;
1314
import java.lang.reflect.Field;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.lang.reflect.Parameter;
1417
import java.util.*;
1518

1619
import static org.junit.jupiter.api.Assertions.assertEquals;
1720
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.*;
2022

2123
public class Module04_Test {
2224
private final String classToFind = "com.h2.MortgageCalculator";
@@ -86,10 +88,45 @@ public void m04_02_testExistenceOfPrivateFields() {
8688
actualFieldsToClass.put(field.getName(), field.getType());
8789
}
8890

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+
}
93130

94131
}
95132
}

0 commit comments

Comments
 (0)