44import org .junit .jupiter .api .Test ;
55import org .mockito .Mockito ;
66import org .springframework .http .HttpStatus ;
7- import org .springframework .http .HttpStatusCode ;
87import org .springframework .http .client .ClientHttpResponse ;
98import org .springframework .web .server .ResponseStatusException ;
109
1312import java .util .stream .Collectors ;
1413
1514import static org .assertj .core .api .Assertions .assertThat ;
16- import static org .junit .jupiter .api .Assertions .*;
15+ import static org .junit .jupiter .api .Assertions .assertThrows ;
16+ import static org .mockito .Mockito .when ;
1717
1818class RestTemplateResponseErrorHandlerTest {
1919
@@ -33,52 +33,58 @@ void should_handle_5xx_http_code() {
3333 // WHEN && THEN
3434 enumSet .forEach (status ->
3535 testTemplate (
36- status ,
37- expectedMessage ,
38- HttpStatus .INTERNAL_SERVER_ERROR
36+ expectedMessage ,
37+ HttpStatus .INTERNAL_SERVER_ERROR
3938 )
4039 );
4140 }
4241
43- @ Test
44- void should_handle_404_http_code () {
45- // GIVEN
46- EnumSet <HttpStatus > enumSet = EnumSet .of (HttpStatus .NOT_FOUND );
47-
48- // WHEN && THEN
49- enumSet .forEach (status -> testTemplate (status , "" , HttpStatus .NOT_FOUND ));
50- }
51-
52- @ Test
53- void should_handle_401_http_code () {
54- // GIVEN
55- EnumSet <HttpStatus > enumSet = EnumSet .of (HttpStatus .UNAUTHORIZED );
56-
57- // WHEN && THEN
58- enumSet .forEach (status -> testTemplate (status , "" , HttpStatus .UNAUTHORIZED ));
42+ private EnumSet <HttpStatus > createEnumSet (Predicate <HttpStatus > predicate ) {
43+ EnumSet <HttpStatus > enumSet = EnumSet .allOf (HttpStatus .class );
44+ return EnumSet .copyOf (
45+ enumSet .stream ()
46+ .filter (predicate )
47+ .collect (Collectors .toSet ())
48+ );
5949 }
6050
61- private void testTemplate (HttpStatus status , String message , HttpStatus responseStatus ) {
51+ private void testTemplate (String message , HttpStatus responseStatus ) {
6252 // GIVEN
6353 ClientHttpResponse response = Mockito .mock (ClientHttpResponse .class );
6454
65- // WHEN && THEN
55+ // WHEN
56+ try {
57+ when (response .getStatusCode ()).thenReturn (responseStatus );
58+ } catch (Exception e ) {
59+ throw new RuntimeException (e );
60+ }
61+
62+ // THEN
6663 ResponseStatusException exception = assertThrows (
6764 ResponseStatusException .class ,
68- () -> restTemplateResponseErrorHandler .handleError (response , status )
65+ () -> restTemplateResponseErrorHandler .handleError (response )
6966 );
7067 assertThat (exception .getMessage ())
7168 .contains (message );
7269 assertThat (exception .getStatusCode ())
7370 .isEqualTo (responseStatus );
7471 }
7572
76- private EnumSet <HttpStatus > createEnumSet (Predicate <HttpStatus > predicate ) {
77- EnumSet <HttpStatus > enumSet = EnumSet .allOf (HttpStatus .class );
78- return EnumSet .copyOf (
79- enumSet .stream ()
80- .filter (predicate )
81- .collect (Collectors .toSet ())
82- );
73+ @ Test
74+ void should_handle_404_http_code () {
75+ // GIVEN
76+ EnumSet <HttpStatus > enumSet = EnumSet .of (HttpStatus .NOT_FOUND );
77+
78+ // WHEN && THEN
79+ enumSet .forEach (status -> testTemplate ("" , HttpStatus .NOT_FOUND ));
80+ }
81+
82+ @ Test
83+ void should_handle_401_http_code () {
84+ // GIVEN
85+ EnumSet <HttpStatus > enumSet = EnumSet .of (HttpStatus .UNAUTHORIZED );
86+
87+ // WHEN && THEN
88+ enumSet .forEach (status -> testTemplate ("" , HttpStatus .UNAUTHORIZED ));
8389 }
8490}
0 commit comments