1
+ package org .junit .internal .runners .statements ;
2
+
3
+ import org .junit .Test ;
4
+ import org .junit .internal .AssumptionViolatedException ;
5
+ import org .junit .runners .model .Statement ;
6
+
7
+ import static org .hamcrest .CoreMatchers .containsString ;
8
+ import static org .hamcrest .CoreMatchers .equalTo ;
9
+ import static org .junit .Assert .assertThat ;
10
+ import static org .junit .Assert .fail ;
11
+
12
+ /**
13
+ * Integration tests can be found in {@link org.junit.tests.running.methods.ExpectedTest}.
14
+ * See e.g. {@link org.junit.tests.running.methods.ExpectedTest#expectsAssumptionViolatedException()}
15
+ */
16
+ public class ExpectExceptionTest {
17
+
18
+ @ Test
19
+ public void whenExpectingAssumptionViolatedExceptionStatementsThrowingItShouldPass () {
20
+ Statement delegate = new Fail (new AssumptionViolatedException ("expected" ));
21
+ ExpectException expectException = new ExpectException (delegate , AssumptionViolatedException .class );
22
+
23
+ try {
24
+ expectException .evaluate ();
25
+ // then AssumptionViolatedException should not be thrown
26
+ } catch (Throwable e ) { // need to explicitly catch and re-throw as an AssertionError or it might be skipped
27
+ fail ("should not throw anything, but was thrown: " + e );
28
+ }
29
+ }
30
+
31
+ @ Test
32
+ public void whenExpectingAssumptionViolatedExceptionStatementsThrowingSubclassShouldPass () {
33
+ Statement delegate = new Fail (new AssumptionViolatedExceptionSubclass ("expected" ));
34
+ ExpectException expectException = new ExpectException (delegate , AssumptionViolatedException .class );
35
+
36
+ try {
37
+ expectException .evaluate ();
38
+ // then no exception should be thrown
39
+ } catch (Throwable e ) {
40
+ fail ("should not throw anything, but was thrown: " + e );
41
+ }
42
+ }
43
+
44
+ @ Test
45
+ public void whenExpectingAssumptionViolatedExceptionStatementsThrowingDifferentExceptionShouldFail () {
46
+ Statement delegate = new Fail (new SomeException ("not expected" ));
47
+ ExpectException expectException = new ExpectException (delegate , AssumptionViolatedException .class );
48
+
49
+ try {
50
+ expectException .evaluate ();
51
+ fail ("should throw 'Unexpected exception' when statement throws an exception which is not the one expected" );
52
+ } catch (Exception e ) {
53
+ assertThat (e .getMessage (), equalTo ("Unexpected exception, expected<org.junit.internal.AssumptionViolatedException> " +
54
+ "but was<org.junit.internal.runners.statements.ExpectExceptionTest$SomeException>" ));
55
+ }
56
+ }
57
+
58
+ @ Test
59
+ public void whenExpectingAssumptionViolatedExceptionStatementsPassingShouldFail () throws Exception {
60
+ ExpectException expectException = new ExpectException (new PassingStatement (), AssumptionViolatedException .class );
61
+
62
+ try {
63
+ expectException .evaluate ();
64
+ } catch (AssertionError e ) {
65
+ assertThat (e .getMessage (), containsString ("Expected exception: " + AssumptionViolatedException .class .getName ()));
66
+ return ;
67
+ }
68
+ fail ("ExpectException should throw when the given statement passes" );
69
+ }
70
+
71
+ private static class PassingStatement extends Statement {
72
+ public void evaluate () throws Throwable {
73
+ // nop
74
+ }
75
+ }
76
+
77
+ private static class SomeException extends RuntimeException {
78
+ public SomeException (String message ) {
79
+ super (message );
80
+ }
81
+ }
82
+
83
+ private static class AssumptionViolatedExceptionSubclass extends AssumptionViolatedException {
84
+ public AssumptionViolatedExceptionSubclass (String assumption ) {
85
+ super (assumption );
86
+ }
87
+ }
88
+ }
0 commit comments