• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitoutil;
6 
7 import static org.assertj.core.api.Assertions.assertThatThrownBy;
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.assertTrue;
10 import static org.junit.Assert.fail;
11 import static org.mockito.Mockito.mock;
12 
13 import org.assertj.core.api.Assertions;
14 import org.junit.Test;
15 import org.junit.rules.MethodRule;
16 import org.junit.runners.model.FrameworkMethod;
17 import org.junit.runners.model.Statement;
18 
19 public class SafeJUnitRuleTest {
20 
21     MethodRuleStub delegate = new MethodRuleStub();
22     SafeJUnitRule rule = new SafeJUnitRule(delegate);
23 
24     @Test
happy_path_no_exception()25     public void happy_path_no_exception() throws Throwable {
26         // when
27         rule.apply(
28                         new Statement() {
29                             public void evaluate() {
30                                 // all good
31                             }
32                         },
33                         mock(FrameworkMethod.class),
34                         this)
35                 .evaluate();
36 
37         // then
38         assertTrue(delegate.statementEvaluated);
39     }
40 
41     @Test
regular_failing_test()42     public void regular_failing_test() {
43         // given
44         Statement baseStatement =
45                 new Statement() {
46                     public void evaluate() {
47                         throw new IllegalArgumentException();
48                     }
49                 };
50         Statement statement = rule.apply(baseStatement, mock(FrameworkMethod.class), this);
51 
52         // when / then
53         assertThatThrownBy(
54                         () -> {
55                             statement.evaluate();
56                         })
57                 .isInstanceOf(IllegalArgumentException.class);
58     }
59 
60     @Test
rule_threw_exception()61     public void rule_threw_exception() throws Throwable {
62         // expect
63         rule.expectFailure(AssertionError.class, "x");
64 
65         // when
66         rule.apply(
67                         new Statement() {
68                             public void evaluate() {
69                                 throw new AssertionError("x");
70                             }
71                         },
72                         mock(FrameworkMethod.class),
73                         this)
74                 .evaluate();
75     }
76 
77     @Test
expected_exception_but_no_exception()78     public void expected_exception_but_no_exception() throws Throwable {
79         // expect
80         rule.expectFailure(AssertionError.class, "x");
81 
82         // when
83         try {
84             rule.apply(
85                             new Statement() {
86                                 public void evaluate() {
87                                     // all good
88                                 }
89                             },
90                             mock(FrameworkMethod.class),
91                             this)
92                     .evaluate();
93             fail();
94 
95             // then
96         } catch (SafeJUnitRule.ExpectedThrowableNotReported t) {
97             // yup, expected
98         }
99     }
100 
101     @Test
expected_exception_message_did_not_match()102     public void expected_exception_message_did_not_match() throws Throwable {
103         // expect
104         rule.expectFailure(AssertionError.class, "FOO");
105 
106         // when
107         try {
108             rule.apply(
109                             new Statement() {
110                                 public void evaluate() {
111                                     throw new AssertionError("BAR");
112                                 }
113                             },
114                             mock(FrameworkMethod.class),
115                             this)
116                     .evaluate();
117             fail();
118         } catch (AssertionError throwable) {
119             Assertions.assertThat(throwable).hasMessageContaining("Expecting message");
120         }
121     }
122 
123     @Test
expected_exception_type_did_not_match()124     public void expected_exception_type_did_not_match() throws Throwable {
125         // expect
126         rule.expectFailure(AssertionError.class, "x");
127 
128         // when
129         try {
130             rule.apply(
131                             new Statement() {
132                                 public void evaluate() {
133                                     throw new RuntimeException("x");
134                                 }
135                             },
136                             mock(FrameworkMethod.class),
137                             this)
138                     .evaluate();
139             fail();
140         } catch (AssertionError throwable) {
141             Assertions.assertThat(throwable).hasMessageContaining("but was:");
142         }
143     }
144 
145     @Test
expected_exception_assert_did_not_match()146     public void expected_exception_assert_did_not_match() throws Throwable {
147         // expect
148         rule.expectFailure(
149                 new SafeJUnitRule.FailureAssert() {
150                     public void doAssert(Throwable t) {
151                         throw new AssertionError("x");
152                     }
153                 });
154 
155         // when
156         try {
157             rule.apply(
158                             new Statement() {
159                                 public void evaluate() {
160                                     throw new RuntimeException();
161                                 }
162                             },
163                             mock(FrameworkMethod.class),
164                             this)
165                     .evaluate();
166             fail();
167         } catch (AssertionError throwable) {
168             assertEquals(throwable.getMessage(), "x");
169         }
170     }
171 
172     private static class MethodRuleStub implements MethodRule {
173         private boolean statementEvaluated;
174 
apply(final Statement base, FrameworkMethod method, Object target)175         public Statement apply(final Statement base, FrameworkMethod method, Object target) {
176             return new Statement() {
177                 public void evaluate() throws Throwable {
178                     statementEvaluated = true;
179                     base.evaluate();
180                 }
181             };
182         }
183     }
184 }
185