View Javadoc
1   package org.junit.internal.matchers;
2   
3   import static org.hamcrest.CoreMatchers.any;
4   import static org.hamcrest.CoreMatchers.containsString;
5   import static org.hamcrest.CoreMatchers.equalTo;
6   import static org.hamcrest.CoreMatchers.notNullValue;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertThat;
9   import static org.junit.Assert.assertTrue;
10  import static org.junit.internal.matchers.StacktracePrintingMatcher.isException;
11  import static org.junit.internal.matchers.StacktracePrintingMatcher.isThrowable;
12  
13  import org.junit.Test;
14  
15  public class StacktracePrintingMatcherTest {
16  
17      @Test
18      public void succeedsWhenInnerMatcherSucceeds() throws Exception {
19          assertTrue(isThrowable(any(Throwable.class)).matches(new Exception()));
20      }
21  
22      @Test
23      public void failsWhenInnerMatcherFails() throws Exception {
24          assertFalse(isException(notNullValue(Exception.class)).matches(null));
25      }
26  
27      @Test
28      public void assertThatIncludesStacktrace() {
29          Exception actual = new IllegalArgumentException("my message");
30          Exception expected = new NullPointerException();
31  
32          try {
33              assertThat(actual, isThrowable(equalTo(expected)));
34          } catch (AssertionError e) {
35              assertThat(e.getMessage(), containsString("Stacktrace was: java.lang.IllegalArgumentException: my message"));
36          }
37      }
38  }