View Javadoc
1   package org.junit;
2   
3   import static org.hamcrest.CoreMatchers.containsString;
4   import static org.hamcrest.CoreMatchers.is;
5   import static org.hamcrest.CoreMatchers.notNullValue;
6   import static org.hamcrest.CoreMatchers.nullValue;
7   import static org.junit.Assert.assertThat;
8   import static org.junit.Assume.assumeThat;
9   import org.hamcrest.Matcher;
10  import org.hamcrest.StringDescription;
11  import org.junit.experimental.theories.DataPoint;
12  import org.junit.experimental.theories.Theories;
13  import org.junit.experimental.theories.Theory;
14  import org.junit.runner.RunWith;
15  
16  @RunWith(Theories.class)
17  public class AssumptionViolatedExceptionTest {
18      @DataPoint
19      public static Integer TWO = 2;
20  
21      @DataPoint
22      public static Matcher<Integer> IS_THREE = is(3);
23  
24      @DataPoint
25      public static Matcher<Integer> NULL = null;
26  
27      @Theory
28      public void toStringReportsMatcher(Integer actual, Matcher<Integer> matcher) {
29          assumeThat(matcher, notNullValue());
30          assertThat(new AssumptionViolatedException(actual, matcher).toString(),
31                  containsString(matcher.toString()));
32      }
33  
34      @Theory
35      public void toStringReportsValue(Integer actual, Matcher<Integer> matcher) {
36          assertThat(new AssumptionViolatedException(actual, matcher).toString(),
37                  containsString(String.valueOf(actual)));
38      }
39  
40      @Test
41      public void assumptionViolatedExceptionWithMatcherDescribesItself() {
42          AssumptionViolatedException e = new AssumptionViolatedException(3, is(2));
43          assertThat(StringDescription.asString(e), is("got: <3>, expected: is <2>"));
44      }
45  
46      @Test
47      public void simpleAssumptionViolatedExceptionDescribesItself() {
48          AssumptionViolatedException e = new AssumptionViolatedException("not enough money");
49          assertThat(StringDescription.asString(e), is("not enough money"));
50      }
51  
52      @Test
53      public void canInitCauseWithInstanceCreatedWithString() {
54        AssumptionViolatedException e = new AssumptionViolatedException("invalid number");
55        Throwable cause = new RuntimeException("cause");
56        e.initCause(cause);
57        assertThat(e.getCause(), is(cause));
58      }
59  
60      @Test
61      @SuppressWarnings("deprecation")
62      public void canSetCauseWithInstanceCreatedWithObjectAndMatcher() {
63        Throwable testObject = new Exception();
64        org.junit.internal.AssumptionViolatedException e
65                = new org.junit.internal.AssumptionViolatedException(
66                        testObject, containsString("test matcher"));
67        assertThat(e.getCause(), is(testObject));
68      }
69  
70      @Test
71      @SuppressWarnings("deprecation")
72      public void canSetCauseWithInstanceCreatedWithAssumptionObjectAndMatcher() {
73        Throwable testObject = new Exception();
74        org.junit.internal.AssumptionViolatedException e
75                = new org.junit.internal.AssumptionViolatedException(
76                        "sample assumption", testObject, containsString("test matcher"));
77        assertThat(e.getCause(), is(testObject));
78      }
79  
80      @Test
81      @SuppressWarnings("deprecation")
82      public void canSetCauseWithInstanceCreatedWithMainConstructor() {
83        Throwable testObject = new Exception();
84        org.junit.internal.AssumptionViolatedException e
85                = new org.junit.internal.AssumptionViolatedException(
86                        "sample assumption", false, testObject, containsString("test matcher"));
87        assertThat(e.getCause(), is(testObject));
88      }
89  
90      @Test
91      public void canSetCauseWithInstanceCreatedWithExplicitThrowableConstructor() {
92        Throwable cause = new Exception();
93        AssumptionViolatedException e = new AssumptionViolatedException("invalid number", cause);
94        assertThat(e.getCause(), is(cause));
95      }
96  }