16
16
17
17
package org .springframework .test .web .servlet ;
18
18
19
- import org .assertj .core .api .Assertions ;
20
19
import org .junit .jupiter .api .Test ;
21
20
21
+ import static org .assertj .core .api .Assertions .assertThat ;
22
22
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
23
23
import static org .assertj .core .api .Assertions .assertThatNoException ;
24
24
31
31
*/
32
32
class ResultMatcherTests {
33
33
34
+ private static final String EOL = "\n " ;
35
+
34
36
private final StubMvcResult stubMvcResult = new StubMvcResult (null , null , null , null , null , null , null );
35
37
36
38
@@ -42,36 +44,74 @@ void softAssertionsWithNoFailures() {
42
44
}
43
45
44
46
@ Test
45
- void softAssertionsWithOneFailure () {
46
- String failureMessage = "failure message " ;
47
- ResultMatcher resultMatcher = ResultMatcher .matchAllSoftly (failingMatcher (failureMessage ));
47
+ void softAssertionsWithOneAssertionError () {
48
+ String failureMessage = "error " ;
49
+ ResultMatcher resultMatcher = ResultMatcher .matchAllSoftly (assertionErrorMatcher (failureMessage ));
48
50
49
51
assertThatExceptionOfType (AssertionError .class )
50
52
.isThrownBy (() -> resultMatcher .match (stubMvcResult ))
51
- .withMessage (failureMessage );
53
+ .withMessage (failureMessage )
54
+ .withNoCause ()
55
+ .satisfies (error -> assertThat (error ).hasNoSuppressedExceptions ());
56
+ }
57
+
58
+ @ Test
59
+ void softAssertionsWithOneRuntimeException () {
60
+ String failureMessage = "exception" ;
61
+ ResultMatcher resultMatcher = ResultMatcher .matchAllSoftly (uncheckedExceptionMatcher (failureMessage ));
62
+
63
+ assertThatExceptionOfType (RuntimeException .class )
64
+ .isThrownBy (() -> resultMatcher .match (stubMvcResult ))
65
+ .withMessage (failureMessage )
66
+ .withNoCause ()
67
+ .satisfies (error -> assertThat (error ).hasNoSuppressedExceptions ());
68
+ }
69
+
70
+ @ Test
71
+ void softAssertionsWithOneCheckedException () {
72
+ String failureMessage = "exception" ;
73
+ ResultMatcher resultMatcher = ResultMatcher .matchAllSoftly (checkedExceptionMatcher (failureMessage ));
74
+
75
+ assertThatExceptionOfType (Exception .class )
76
+ .isThrownBy (() -> resultMatcher .match (stubMvcResult ))
77
+ .withMessage (failureMessage )
78
+ .withNoCause ()
79
+ .satisfies (exception -> assertThat (exception ).hasNoSuppressedExceptions ());
52
80
}
53
81
54
82
@ Test
55
83
void softAssertionsWithTwoFailures () {
56
84
String firstFailure = "firstFailure" ;
57
85
String secondFailure = "secondFailure" ;
58
- ResultMatcher resultMatcher = ResultMatcher .matchAllSoftly (failingMatcher (firstFailure ), exceptionalMatcher (secondFailure ));
86
+ String thirdFailure = "thirdFailure" ;
87
+ ResultMatcher resultMatcher = ResultMatcher .matchAllSoftly (assertionErrorMatcher (firstFailure ),
88
+ checkedExceptionMatcher (secondFailure ), uncheckedExceptionMatcher (thirdFailure ));
59
89
60
90
assertThatExceptionOfType (AssertionError .class )
61
91
.isThrownBy (() -> resultMatcher .match (stubMvcResult ))
62
- .withMessage (firstFailure + System .lineSeparator () + secondFailure );
92
+ .withMessage ("Multiple Exceptions (3):" + EOL + firstFailure + EOL + secondFailure + EOL + thirdFailure )
93
+ .satisfies (error -> assertThat (error .getSuppressed ()).hasSize (3 ));
63
94
}
64
95
65
- private ResultMatcher failingMatcher (String failureMessage ) {
66
- return result -> Assertions .fail (failureMessage );
96
+ private ResultMatcher assertionErrorMatcher (String failureMessage ) {
97
+ return result -> {
98
+ throw new AssertionError (failureMessage );
99
+ };
67
100
}
68
101
69
- private ResultMatcher exceptionalMatcher (String failureMessage ) {
102
+ private ResultMatcher uncheckedExceptionMatcher (String failureMessage ) {
70
103
return result -> {
71
104
throw new RuntimeException (failureMessage );
72
105
};
73
106
}
74
107
75
- void doNothing (MvcResult mvcResult ) {}
108
+ private ResultMatcher checkedExceptionMatcher (String failureMessage ) {
109
+ return result -> {
110
+ throw new Exception (failureMessage );
111
+ };
112
+ }
113
+
114
+ void doNothing (MvcResult mvcResult ) {
115
+ }
76
116
77
117
}
0 commit comments