I want to write unit tests for the parser and would like to check if it throws an exception correctly for all input lines in the list. Now, as I understand it, the standard approach with JUnit would be to write a separate test method for each case:
public final class ParseFailureTest1 { @Test(expected = ParseException.class) public void testParseFailure1() throws Exception { Parser.parse("[1 2]");
But since I want to apply the same test to 20 or 50 different lines, this seems impractical.
An alternative would be to explicitly check for an exception with a catch :
public final class ParseFailureTest2 { @Test public void testParseFailure() throws Exception { List<String> documents = Arrays.asList( "[1 2]",
But this is a mistake, and I do not get any information about which exception was expected, and if another exception was selected, it will be considered a test error, not an error.
My solution would be to use a method similar to expectException below:
public final class ParseFailureTest3 { @Test public void testParseFailure() throws Exception { List<String> documents = Arrays.asList( "[1 2]", // Missing comma "[1, 2,]"); // Additional commas for (final String document : documents) { expectException(ParseException.class, new TestRunnable() { @Override public void run() throws Throwable { Parser.parse(document); } }); } } public static void expectException(Class<? extends Throwable> expected, TestRunnable test) { try { test.run(); } catch (Throwable e) { if (e.getClass() == expected) { return; // Expected, do nothing. } else { throw new AssertionError(String.format("Wrong exception was thrown: %s instead of %s", e.getClass(), expected), e); } } throw new AssertionError(String.format("Expected exception was not thrown: %s", expected)); } public interface TestRunnable { void run() throws Throwable; } }
Is there a method for this purpose in the JUnit framework or related library, or would you suggest a different approach (or one of my rejected approaches) to the problem?