title: Testing for the expected exception message with junit 5
body: <p>I have a project where I have tests where I deliberately cause a problem and then verify the code responds the way I want it. For this I want to be sure the exceptions not only are the right class but they must also carry the right message.</p>
  
  <p>So in one of <a href="https://github.com/nielsbasjes/yauaa/blob/v5.12/analyzer/src/test/java/nl/basjes/parse/useragent/utils/TestVersionCollisionChecks.java#L76" rel="noreferrer">my existing (junit 4) tests</a> I have something similar to this:</p>
  
  <pre><code>public class MyTests {
    @Rule
    public final ExpectedException expectedEx = ExpectedException.none();
  
    @Test
    public void testLoadingResourcesTheBadWay(){
      expectedEx.expect(MyCustomException.class);
      expectedEx.expectMessage(allOf(startsWith("Unable to load "), endsWith(" resources.")));
      doStuffThatShouldFail();
    }
  }
  </code></pre>
  
  <p>I'm currently looking into fully migrating to junit 5 which no longer supports the <a href="https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-tips" rel="noreferrer">@Rule</a> and now has the <a href="https://junit.org/junit5/docs/5.5.0/api/org/junit/jupiter/api/Assertions.html#assertThrows(java.lang.Class,org.junit.jupiter.api.function.Executable)" rel="noreferrer">assertThrows that seems to replace this</a>.</p>
  
  <p>What I have not been able to figure out how to write a test that not only checks the exception(class) that is thrown but also the message attached to that exception.</p>
  
  <p>What is the proper way to write such a test in Junit 5?</p>