title: How to use jUnit 5 Assertions to check, whether exception message starts with a String?
body: <p>I use <code>org.junit.jupiter.api.Assertions</code> object to assert an exception is thrown:</p>
  
  <pre><code>Assertions.assertThrows(
          InvalidParameterException.class,
          () -&gt; new ThrowingExceptionClass().doSomethingDangerous());
  </code></pre>
  
  <p>Simplified, the exception thrown has a variable part <code>dateTime</code> in its message:</p>
  
  <pre><code>final String message = String.format("Either request is too old [dateTime=%s]", date);
  new InvalidParameterException(message);
  </code></pre>
  
  <p>As of version I use <code>5.4.0</code> the <a href="https://junit.org/junit5/docs/5.4.0/api/org/junit/jupiter/api/Assertions.html" rel="noreferrer"><code>Assertions</code></a> provide three methods checking the exception is thrown:</p>
  
  <ul>
  <li><a href="https://junit.org/junit5/docs/5.4.0/api/org/junit/jupiter/api/Assertions.html#assertThrows(java.lang.Class,org.junit.jupiter.api.function.Executable)" rel="noreferrer"><code>assertThrows​(Class&lt;T&gt; expectedType, Executable executable)</code></a></li>
  <li><a href="https://junit.org/junit5/docs/5.4.0/api/org/junit/jupiter/api/Assertions.html#assertThrows(java.lang.Class,org.junit.jupiter.api.function.Executable,java.lang.String)" rel="noreferrer"><code>assertThrows​(Class&lt;T&gt; expectedType, Executable executable, String message)</code></a></li>
  <li><a href="https://junit.org/junit5/docs/5.4.0/api/org/junit/jupiter/api/Assertions.html#assertThrows(java.lang.Class,org.junit.jupiter.api.function.Executable,java.util.function.Supplier)" rel="noreferrer"><code>assertThrows​(Class&lt;T&gt; expectedType, Executable executable, Supplier&lt;String&gt; messageSupplier)</code></a></li>
  </ul>
  
  <p>Neither of them provides a mechanism checking whether a String <strong>starts with</strong> another String. The last 2 methods only check whether the String is equal. How do I check easily whether the exception message starts with <code>"Either request is too old"</code> since more message variations might occur within the same <code>InvalidParameterException</code>?</p>
  
  <hr>
  
  <p>I'd appreciate a method <code>assertThrows​(Class&lt;T&gt; expectedType, Executable executable, Predicate&lt;String&gt; messagePredicate)</code> where the predicate would provide the thrown <code>message</code> and the assertion passes when if predicate returns <code>true</code> such as:</p>
  
  <pre><code>Assertions.assertThrows(
      InvalidParameterException.class,
      () -&gt; new ThrowingExceptionClass().doSomethingDangerous()
      message -&gt; message.startsWith("Either request is too old"));
  </code></pre>
  
  <p>Sadly, it doesn't exist. Any workaround?</p>