Skip to content

Commit

Permalink
Added m6_06_testMainMethodPrintsCorrectMortgageAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
hhimanshu committed May 24, 2020
1 parent 1f11877 commit 9287a2a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/test/java/com/h2/Module04_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static org.junit.platform.commons.util.ReflectionUtils.*;

public class Module04_Test {
private final String classToFind = "com.h2.MortgageCalculator";
private static final String classToFind = "com.h2.MortgageCalculator";
private final InputStream systemIn = System.in;
private final PrintStream systemOut = System.out;

Expand Down Expand Up @@ -50,7 +50,7 @@ private static Optional<Class<?>> getClass(final String className) {
return aClass.toOptional();
}

public Optional<Class<?>> getMortgageClass() {
public static Optional<Class<?>> getMortgageClass() {
return getClass(classToFind);
}

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/h2/Module06_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.platform.commons.util.ReflectionUtils.*;
Expand Down Expand Up @@ -131,4 +134,43 @@ public void m6_05_testGetFloatValueExistenceAndCorrectness() throws IllegalAcces
fail("Can't find a method with name " + methodName + " in class " + classToFind + " with 1 parameter of type 'String'");
}
}

@Test
public void m6_06_testMainMethodPrintsCorrectMortgageAmount() throws NoSuchMethodException {
final String methodName = "main";

final Optional<Class<?>> maybeMortgageCalculator = Module04_Test.getMortgageClass();
assertTrue(maybeMortgageCalculator.isPresent(), classToFind + " must exist");
final Class<?> mortgageCalculator = maybeMortgageCalculator.get();

final Method[] methods = mortgageCalculator.getDeclaredMethods();

final List<Method> 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 String loanAmount = "264000";
final String termInYears = "30";
final String annualRate = "3.74";
String input = "hello";

Method method = mortgageCalculator.getMethod("main", String[].class);
{
final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, (Object) new String[]{input, termInYears, annualRate}));
Throwable targetException = exception.getTargetException();
assertEquals(IllegalArgumentException.class, targetException.getClass(), methodName + " should have thrown an instance of 'IllegalArgumentException'");
assertEquals(input + " cannot be converted into a 'long' value. Exiting program.", targetException.getMessage());
}
{
final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, (Object) new String[]{loanAmount, input, annualRate}));
Throwable targetException = exception.getTargetException();
assertEquals(IllegalArgumentException.class, targetException.getClass(), methodName + " should have thrown an instance of 'IllegalArgumentException'");
assertEquals(input + " cannot be converted into a 'int' value. Exiting program.", targetException.getMessage());
}
{
final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, (Object) new String[]{loanAmount, termInYears, input}));
Throwable targetException = exception.getTargetException();
assertEquals(IllegalArgumentException.class, targetException.getClass(), methodName + " should have thrown an instance of 'IllegalArgumentException'");
assertEquals(input + " cannot be converted into a 'float' value. Exiting program.", targetException.getMessage());
}
}
}

0 comments on commit 9287a2a

Please sign in to comment.