Skip to content

Commit

Permalink
Added m6_04_testGetIntValueExistenceAndCorrectness
Browse files Browse the repository at this point in the history
  • Loading branch information
hhimanshu committed May 24, 2020
1 parent 66d21bb commit f3e065a
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/test/java/com/h2/Module06_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,38 @@ public void m6_03_testGetLongValueCorrectness() throws IllegalAccessException, I
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());
}

} catch (NoSuchMethodException e) {
fail("Can't find a method with name " + methodName + " in class " + classToFind + " with 1 parameter of type 'String'");
}
}

@Test
public void m6_04_testGetIntValueExistenceAndCorrectness() throws IllegalAccessException, InvocationTargetException {
final String methodName = "getIntValue";
final Optional<Class<?>> maybeClass = getUtilitiesClass();
assertTrue(maybeClass.isPresent(), classToFind + " should be present");
assertEquals(classToFind, maybeClass.get().getCanonicalName());

final Class<?> aClass = maybeClass.get();
try {
Method method = aClass.getMethod(methodName, String.class);
assertTrue(isPublic(method), methodName + " must be declared 'public'");
assertTrue(isStatic(method), methodName + " must be declared 'static'");
assertEquals(int.class, method.getReturnType(), methodName + " must have a 'int' return type");

{
int result = (int) method.invoke(null, "100");
assertEquals(100, result, methodName + " should convert '100' into '100'");
}
{
String input = "hello";
final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, 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 'int' value. Exiting program.", targetException.getMessage());
}
} catch (NoSuchMethodException e) {
fail("Can't find a method with name " + methodName + " in class " + classToFind + " with 1 parameter of type 'String'");
}
}
}

0 comments on commit f3e065a

Please sign in to comment.