Last active
November 29, 2018 17:09
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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