title: Unable to expect multiple exceptions with JUnit's ExpectedException
body: <ul>
  <li>Java 7</li>
  <li>JUnit 4.11</li>
  </ul>
  
  <p>I'm writing tests to validate my methods' contracts, e.g., a parameter cannot be <code>null</code>, an <code>int</code> param must be positive, etc. When a contract is violated, the thrown exception type reflects why it failed, such as <code>NullPointerException</code> or <code>IllegalArgumentException</code>. Sometimes I don't care which of these two is thrown, only that <em>one of them</em> is thrown. (Yes, I <em>should</em> be testing for the exact exception type, but bear with me...)</p>
  
  <p>I could use JUnit's <code>@Test(expected = RuntimeException.class)</code> (since <code>RuntimeException</code> is the closest common parent of <code>IAE</code> and <code>NPE</code>), but this is overly generic as the method under test might throw some other <code>RuntimeException</code>.</p>
  
  <p>Instead, I'm trying to use JUnit's <a href="http://junit.org/javadoc/latest/org/junit/rules/ExpectedException.html" rel="nofollow"><code>ExpectedExceptions</code></a> class.</p>
  
  <p>Here is a complete example:</p>
  
  <pre><code>import org.hamcrest.CoreMatchers;
  import org.junit.Rule;
  import org.junit.Test;
  import org.junit.rules.ExpectedException;
  import static org.hamcrest.CoreMatchers.anyOf;
  
  public class ParameterViolationTest {
      private Target target = new Target();
  
      @Rule
      public ExpectedException expected = ExpectedException.none();
  
      @Test
      public void parameterViolation() {
          expected.expect(anyOf(
                  CoreMatchers.&lt;Class&lt;? extends Exception&gt;&gt;
                          equalTo(NullPointerException.class),
                  CoreMatchers.&lt;Class&lt;? extends Exception&gt;&gt;
                          equalTo(IllegalArgumentException.class)));
  
          // Null parameter violates contract, throws some exception.
          target.methodUnderTest(null);
      }
  
      private static class Target {
          public void methodUnderTest(Object o) {
              if (o == null) {
                  // throw new IllegalArgumentException();
                  throw new NullPointerException();
              }
          }
      }
  }
  </code></pre>
  
  <p>I would expect this test to pass, but instead it fails:</p>
  
  <pre class="lang-none prettyprint-override"><code>java.lang.AssertionError: 
  Expected: (&lt;class java.lang.NullPointerException&gt; or &lt;class java.lang.IllegalArgumentException&gt;)
       but: was &lt;java.lang.NullPointerException&gt;
  Stacktrace was: java.lang.NullPointerException
      at ParameterViolationTest$Target.methodUnderTest(ParameterViolationTest.java:35)
      at ParameterViolationTest.parameterViolation(ParameterViolationTest.java:25)
      &lt; internal calls... &gt;
      at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168)
      at org.junit.rules.RunRules.evaluate(RunRules.java:20)
      &lt; internal calls... &gt;
  java.lang.AssertionError: 
  Expected: (&lt;class java.lang.NullPointerException&gt; or &lt;class java.lang.IllegalArgumentException&gt;)
       but: was &lt;java.lang.NullPointerException&gt;
  Stacktrace was: java.lang.NullPointerException
      at ParameterViolationTest$Target.methodUnderTest(ParameterViolationTest.java:35)
      at ParameterViolationTest.parameterViolation(ParameterViolationTest.java:25)
      &lt; internal calls... &gt;
      at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168)
      at org.junit.rules.RunRules.evaluate(RunRules.java:20)
      &lt; internal calls... &gt;
      at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
      at org.junit.Assert.assertThat(Assert.java:865)
      at org.junit.Assert.assertThat(Assert.java:832)
      at org.junit.rules.ExpectedException.handleException(ExpectedException.java:198)
      at org.junit.rules.ExpectedException.access$500(ExpectedException.java:85)
      at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:177)
      at org.junit.rules.RunRules.evaluate(RunRules.java:20)
      &lt; internal calls... &gt;
  </code></pre>
  
  <p>What's going on here?</p>