View Javadoc
1   package org.junit.tests.experimental.rules;
2   
3   import static org.hamcrest.CoreMatchers.is;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertThat;
6   import static org.junit.experimental.results.PrintableResult.testResult;
7   import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
8   import static org.junit.experimental.results.ResultMatchers.isSuccessful;
9   
10  import java.util.concurrent.Callable;
11  
12  import org.junit.Rule;
13  import org.junit.Test;
14  import org.junit.experimental.results.PrintableResult;
15  import org.junit.rules.ErrorCollector;
16  import org.junit.rules.Verifier;
17  
18  public class VerifierRuleTest {
19      public static class UsesErrorCollector {
20          @Rule
21          public ErrorCollector collector = new ErrorCollector();
22  
23          @Test
24          public void example() {
25              collector.addError(new Throwable("message"));
26          }
27      }
28  
29      @Test
30      public void usedErrorCollectorShouldFail() {
31          assertThat(testResult(UsesErrorCollector.class), hasFailureContaining("message"));
32      }
33  
34      public static class UsesErrorCollectorTwice {
35          @Rule
36          public ErrorCollector collector = new ErrorCollector();
37  
38          @Test
39          public void example() {
40              collector.addError(new Throwable("first thing went wrong"));
41              collector.addError(new Throwable("second thing went wrong"));
42          }
43      }
44  
45      @Test
46      public void usedErrorCollectorTwiceShouldFail() {
47          PrintableResult testResult = testResult(UsesErrorCollectorTwice.class);
48          assertThat(testResult, hasFailureContaining("first thing went wrong"));
49          assertThat(testResult, hasFailureContaining("second thing went wrong"));
50      }
51  
52      public static class UsesErrorCollectorCheckThat {
53          @Rule
54          public ErrorCollector collector = new ErrorCollector();
55  
56          @Test
57          public void example() {
58              collector.checkThat(3, is(4));
59              collector.checkThat(5, is(6));
60              collector.checkThat("reason 1", 7, is(8));
61              collector.checkThat("reason 2", 9, is(16));
62          }
63      }
64  
65      @Test
66      public void usedErrorCollectorCheckThatShouldFail() {
67          PrintableResult testResult = testResult(UsesErrorCollectorCheckThat.class);
68          assertThat(testResult, hasFailureContaining("was <3>"));
69          assertThat(testResult, hasFailureContaining("was <5>"));
70          assertThat(testResult, hasFailureContaining("reason 1"));
71          assertThat(testResult, hasFailureContaining("was <7>"));
72          assertThat(testResult, hasFailureContaining("reason 2"));
73          assertThat(testResult, hasFailureContaining("was <9>"));
74      }
75  
76      public static class UsesErrorCollectorCheckSucceeds {
77          @Rule
78          public ErrorCollector collector = new ErrorCollector();
79  
80          @Test
81          public void example() {
82              collector.checkSucceeds(new Callable<Object>() {
83                  public Object call() throws Exception {
84                      throw new RuntimeException("first!");
85                  }
86              });
87              collector.checkSucceeds(new Callable<Object>() {
88                  public Object call() throws Exception {
89                      throw new RuntimeException("second!");
90                  }
91              });
92          }
93      }
94  
95      @Test
96      public void usedErrorCollectorCheckSucceedsShouldFail() {
97          PrintableResult testResult = testResult(UsesErrorCollectorCheckSucceeds.class);
98          assertThat(testResult, hasFailureContaining("first!"));
99          assertThat(testResult, hasFailureContaining("second!"));
100     }
101 
102     public static class UsesErrorCollectorCheckSucceedsPasses {
103         @Rule
104         public ErrorCollector collector = new ErrorCollector();
105 
106         @Test
107         public void example() {
108             assertEquals(3, collector.checkSucceeds(new Callable<Object>() {
109                 public Object call() throws Exception {
110                     return 3;
111                 }
112             }));
113         }
114     }
115 
116     @Test
117     public void usedErrorCollectorCheckSucceedsShouldPass() {
118         PrintableResult testResult = testResult(UsesErrorCollectorCheckSucceedsPasses.class);
119         assertThat(testResult, isSuccessful());
120     }
121 
122     private static String sequence;
123 
124     public static class UsesVerifier {
125         @Rule
126         public Verifier collector = new Verifier() {
127             @Override
128             protected void verify() {
129                 sequence += "verify ";
130             }
131         };
132 
133         @Test
134         public void example() {
135             sequence += "test ";
136         }
137     }
138 
139     @Test
140     public void verifierRunsAfterTest() {
141         sequence = "";
142         assertThat(testResult(UsesVerifier.class), isSuccessful());
143         assertEquals("test verify ", sequence);
144     }
145 }