title: Get Exception object in Unit Test with Spring Boot Starter Test
body: <p>In my program I'm throwing a custom Exception object <code>MyCustomException</code>, which looks like this:</p>
  
  <pre><code>public class MyCustomException
  {
      private MyCustomExceptionObject myCustomExceptionObject;
      // Getters, Setters, Constructors...
  }
  
  public class MyCustomExceptionObject
  {
      int code;
      // Getters, Setters, Constructors...
  }
  </code></pre>
  
  <p>With Spring Boot Starter Test I've got many testing libraries at my disposal.</p>
  
  <ul>
  <li>AssertJ</li>
  <li>JUnit4</li>
  <li>Mockito</li>
  <li>Hamcrest</li>
  </ul>
  
  <p>Currently I'm mainly using AssertJ. One of my tests give invalid parameters to a method and expect an exception.</p>
  
  <pre><code>@Test
  public void test()
  {
      org.assertj.core.api.Assertions.assertThatThrownBy(() -&gt; someMethod(-1)).isExactlyInstanceOf(MyCustomException.class);
  }
  
  public void someMethod(int number)
  {
      if (number &lt; 0)
      {
          throw new MyCustomException(new MyCustomExceptionObject(12345));
      }
      //else do something useful
  }
  </code></pre>
  
  <p>This works fine, but I want to test the exceptions more specifically, testing if the <code>code</code> is as expected. Something like this would work:</p>
  
  <pre><code>try
  {
      someMethod(-1);
  }
  catch (MyCustomException e)
  {
      if (e.getCode() == 12345)
      {
          return;
      }
  }
  
  org.assertj.core.api.Assertions.fail("Exception not thrown");
  </code></pre>
  
  <p>but I'm rather looking for a one-liner like:</p>
  
  <p><code>org.assertj.core.api.Assertions.assertThatThrownBy(() -&gt; someMethod(-1)).isExactlyInstanceOf(MyCustomException.class).exceptionIs((e) -&gt; e.getCode() == 12345);</code></p>
  
  <p>Does something like this exist in any of the above listed testing libraries (AssertJ preferred)?</p>