• 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 org.assertj.core.api.Assertions;
8 import org.junit.Test;
9 import org.junit.rules.MethodRule;
10 import org.junit.runners.model.FrameworkMethod;
11 import org.junit.runners.model.Statement;
12 
13 import static org.junit.Assert.*;
14 import static org.mockito.Mockito.mock;
15 
16 public class SafeJUnitRuleTest {
17 
18     MethodRuleStub delegate = new MethodRuleStub();
19     SafeJUnitRule rule = new SafeJUnitRule(delegate);
20 
happy_path_no_exception()21     @Test public void happy_path_no_exception() throws Throwable {
22         //when
23         rule.apply(new Statement() {
24             public void evaluate() throws Throwable {
25                 //all good
26             }
27         }, mock(FrameworkMethod.class), this).evaluate();
28 
29         //then
30         assertTrue(delegate.statementEvaluated);
31     }
32 
33     @Test(expected = IllegalArgumentException.class)
regular_failing_test()34     public void regular_failing_test() throws Throwable {
35         //when
36         rule.apply(new Statement() {
37             public void evaluate() throws Throwable {
38                 throw new IllegalArgumentException();
39             }
40         }, mock(FrameworkMethod.class), this).evaluate();
41     }
42 
rule_threw_exception()43     @Test public void rule_threw_exception() throws Throwable {
44         //expect
45         rule.expectFailure(AssertionError.class, "x");
46 
47         //when
48         rule.apply(new Statement() {
49             public void evaluate() throws Throwable {
50                 throw new AssertionError("x");
51             }
52         }, mock(FrameworkMethod.class), this).evaluate();
53     }
54 
expected_exception_but_no_exception()55     @Test public void expected_exception_but_no_exception() throws Throwable {
56         //expect
57         rule.expectFailure(AssertionError.class, "x");
58 
59         //when
60         try {
61             rule.apply(new Statement() {
62                 public void evaluate() throws Throwable {
63                     //all good
64                 }
65             }, mock(FrameworkMethod.class), this).evaluate();
66             fail();
67 
68         //then
69         } catch (SafeJUnitRule.ExpectedThrowableNotReported t) {
70             //yup, expected
71         }
72     }
73 
expected_exception_message_did_not_match()74     @Test public void expected_exception_message_did_not_match() throws Throwable {
75         //expect
76         rule.expectFailure(AssertionError.class, "FOO");
77 
78         //when
79         try {
80             rule.apply(new Statement() {
81                 public void evaluate() throws Throwable {
82                     throw new AssertionError("BAR");
83                 }
84             }, mock(FrameworkMethod.class), this).evaluate();
85             fail();
86         } catch (AssertionError throwable) {
87             Assertions.assertThat(throwable).hasMessageContaining("Expecting message");
88         }
89     }
90 
expected_exception_type_did_not_match()91     @Test public void expected_exception_type_did_not_match() throws Throwable {
92         //expect
93         rule.expectFailure(AssertionError.class, "x");
94 
95         //when
96         try {
97             rule.apply(new Statement() {
98                 public void evaluate() throws Throwable {
99                     throw new RuntimeException("x");
100                 }
101             }, mock(FrameworkMethod.class), this).evaluate();
102             fail();
103         } catch (AssertionError throwable) {
104             Assertions.assertThat(throwable).hasMessageContaining("but was:");
105         }
106     }
107 
expected_exception_assert_did_not_match()108     @Test public void expected_exception_assert_did_not_match() throws Throwable {
109         //expect
110         rule.expectFailure(new SafeJUnitRule.FailureAssert() {
111             public void doAssert(Throwable t) {
112                 throw new AssertionError("x");
113             }
114         });
115 
116         //when
117         try {
118             rule.apply(new Statement() {
119                 public void evaluate() throws Throwable {
120                     throw new RuntimeException();
121                 }
122             }, mock(FrameworkMethod.class), this).evaluate();
123             fail();
124         } catch (AssertionError throwable) {
125             assertEquals(throwable.getMessage(), "x");
126         }
127     }
128 
129     private static class MethodRuleStub implements MethodRule {
130         private boolean statementEvaluated;
apply(final Statement base, FrameworkMethod method, Object target)131         public Statement apply(final Statement base, FrameworkMethod method, Object target) {
132             return new Statement() {
133                 public void evaluate() throws Throwable {
134                     statementEvaluated = true;
135                     base.evaluate();
136                 }
137             };
138         }
139     }
140 }
141