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