View Javadoc
1   package org.junit.tests.experimental;
2   
3   import static org.hamcrest.CoreMatchers.not;
4   import static org.junit.Assert.assertThat;
5   import static org.junit.Assume.assumeThat;
6   import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
7   import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
8   
9   import java.util.Arrays;
10  
11  import org.hamcrest.Matcher;
12  import org.junit.experimental.results.PrintableResult;
13  import org.junit.experimental.theories.DataPoint;
14  import org.junit.experimental.theories.Theories;
15  import org.junit.experimental.theories.Theory;
16  import org.junit.runner.Description;
17  import org.junit.runner.RunWith;
18  import org.junit.runner.notification.Failure;
19  
20  @RunWith(Theories.class)
21  public class MatcherTest {
22      @DataPoint
23      public static Matcher<Object> SINGLE_FAILURE = hasSingleFailureContaining("cheese");
24  
25      @DataPoint
26      public static Matcher<PrintableResult> ANY_FAILURE = hasFailureContaining("cheese");
27  
28      @DataPoint
29      public static PrintableResult TWO_FAILURES_ONE_CHEESE = new PrintableResult(
30              Arrays.asList(failure("cheese"), failure("mustard")));
31  
32      @Theory
33      @SuppressWarnings({ "unchecked", "rawtypes" })
34      public void differentMatchersHaveDifferentDescriptions(
35              Matcher matcher1, Matcher matcher2, Object value) {
36          assumeThat(value, matcher1);
37          assumeThat(value, not(matcher2));
38          assertThat(matcher1.toString(), not(matcher2.toString()));
39      }
40  
41      private static Failure failure(String string) {
42          return new Failure(Description.EMPTY, new Error(string));
43      }
44  }