Skip to content

Commit

Permalink
Added m4_11_testToStringCorrectness
Browse files Browse the repository at this point in the history
  • Loading branch information
hhimanshu committed May 22, 2020
1 parent faf0c80 commit dfb50a7
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/test/java/com/h2/Module04_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,54 @@ public void m4_10_testExistenceOfToString() {
assertEquals(String.class, method.getReturnType(), methodName + " method must return a value of type 'String'");
}

@Test
public void m4_11_testToStringCorrectness() throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
final Optional<Class<?>> 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<Method> 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);
}

{
final String methodName = "toString";
final List<Method> 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);
final String result = (String) invokeMethod(method, instance);
final String expected = "monthlyPayment: 1221.14";
assertEquals(expected, result, methodName + " should return " + expected);
}
}

@Test
public void m3_xx_testMainMethodExists() {
final String methodName = "main";
Expand Down

0 comments on commit dfb50a7

Please sign in to comment.