title: Check that a method throws an exception when applied to any element of a list of values
body: <p>I want to write unit tests for a parser and would like to check that it correctly throws an exception for all input strings in a list. Now as I understand it, the standard approach with <em>JUnit</em> would be to write a separate test method for each case:</p>
  
  <pre><code>public final class ParseFailureTest1 {
      @Test(expected = ParseException.class)
      public void testParseFailure1() throws Exception {
          Parser.parse("[1 2]"); // Missing comma
      }
  
      @Test(expected = ParseException.class)
      public void testParseFailure2() throws Exception {
          Parser.parse("[1, 2,]"); // Additional commas
      }
  }
  </code></pre>
  
  <p>But as I want to apply the same test to 20 or 50 different strings, it seems impractical.</p>
  
  <p>An alternative would be to explicitly check for an exception with a <code>catch</code> block:</p>
  
  <pre><code>public final class ParseFailureTest2 {
      @Test
      public void testParseFailure() throws Exception {
          List&lt;String&gt; documents = Arrays.asList(
              "[1 2]", // Missing comma
              "[1, 2,]"); // Additional commas
  
          for (String document : documents) {
              try {
                  Parser.parse(document);
  
                  throw new AssertionError("Exception was not thrown");
              } catch (ParseException e) {
                  // Expected, do nothing.
              }
          }
      }
  }
  </code></pre>
  
  <p>But this is error prone and I won't get any information about which exception was expected and if a different exception was thrown, it would count as a test error and not a failure.</p>
  
  <p>My solution would be to use a method similar to <code>expectException</code> below:</p>
  
  <pre><code>public final class ParseFailureTest3 {
      @Test
      public void testParseFailure() throws Exception {
          List&lt;String&gt; 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&lt;? extends Throwable&gt; 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;
      }
  }
  </code></pre>
  
  <p>Is there a method for that purpose in the JUnit framework or a related library or would you suggest a different approach (or one of my rejected approaches) to the problem?</p>