title: Kotlin and JUnit 5 assert an exception is thrown: separate declaration and execution with assertFailsWith
body: <p>In Kotlin with JUnit5 we can use <a href="https://stackoverflow.com/questions/30331806/test-expected-exceptions-in-kotlin">assertFailsWith</a>  </p>
  
  <p>In Java with JUnit5 you can use <a href="https://stackoverflow.com/questions/40268446/junit-5-how-to-assert-an-exception-is-thrown">assertThrows</a>  </p>
  
  <p>In Java, if I want to <strong>separate the declaration of an executable from the execution itself</strong>, in order to clarify the tests in a Given-Then-When form, we can use JUnit5 <code>assertThrows</code> like this:</p>
  
  <pre><code>@Test
  @DisplayName("display() with wrong argument command should fail" )
  void displayWithWrongArgument() {
  
      // Given a wrong argument
      String arg = "FAKE_ID"
  
      // When we call display() with the wrong argument
      Executable exec = () -&gt; sut.display(arg);
  
      // Then it should throw an IllegalArgumentException
      assertThrows(IllegalArgumentException.class, exec);
  }
  </code></pre>
  
  <p>In Kotlin we can use <code>assertFailsWith</code>:  </p>
  
  <pre><code>@Test
  fun `display() with wrong argument command should fail`() {
  
      // Given a wrong argument
      val arg = "FAKE_ID"
  
      // When we call display() with the wrong argument
      // ***executable declaration should go here ***
  
      // Then it should throw an IllegalArgumentException
      assertFailsWith&lt;CrudException&gt; { sut.display(arg) }
  }
  </code></pre>
  
  <p>But, <strong>how we can separate the declaration and the execution in Kotlin with <code>assertFailsWith</code>?</strong></p>