Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
Last active November 29, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CompSciRocks/1324e094891f425b65d4a40be43a3682 to your computer and use it in GitHub Desktop.
Save CompSciRocks/1324e094891f425b65d4a40be43a3682 to your computer and use it in GitHub Desktop.
How to wrap a catch for InvocationTargetException in a junit test - https://compsci.rocks/invocationtargetexception-fix/
public void test_MeetAtMidnight() throws Exception {
RouteCipher rc = new RouteCipher( 3, 5 );
setField( rc, "numRows", 3 );
setField( rc, "numCols", 5 );
Method m = RouteCipher.class.getDeclaredMethod( "fillBlock", new Class[]{ String.class } );
m.setAccessible( true );
// This is the problem line
m.invoke( rc, "Meet at midnight" );
String[][] exp = { { "M", "e", "e", "t", " " }, { "a", "t", " ", "m", "i" }, { "d", "n", "i", "g", "h" } };
String[][] act = getField( rc, "letterBlock" );
assertArrayEquals( "fillBlock failed", exp, act );
}
public void test_MeetAtMidnight() throws Exception {
RouteCipher rc = new RouteCipher( 3, 5 );
setField( rc, "numRows", 3 );
setField( rc, "numCols", 5 );
Method m = RouteCipher.class.getDeclaredMethod( "fillBlock", new Class[]{ String.class } );
m.setAccessible( true );
try {
m.invoke( rc, "Meet at midnight" );
} catch ( InvocationTargetException e ) {
throw (Exception) e.getCause();
}
String[][] exp = { { "M", "e", "e", "t", " " }, { "a", "t", " ", "m", "i" }, { "d", "n", "i", "g", "h" } };
String[][] act = getField( rc, "letterBlock" );
assertArrayEquals( "fillBlock failed", exp, act );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment