1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.exception.spring.boot.autoconfigure.reactive;
18
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.assertj.core.api.Assertions.assertThatNoException;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24
25 import org.bremersee.exception.RestApiExceptionMapper;
26 import org.bremersee.exception.RestApiExceptionMapperForWebFlux;
27 import org.bremersee.exception.spring.boot.autoconfigure.RestApiExceptionMapperBootProperties;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.springframework.beans.factory.ObjectProvider;
31 import org.springframework.boot.autoconfigure.web.WebProperties;
32 import org.springframework.boot.web.reactive.error.ErrorAttributes;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.http.codec.ServerCodecConfigurer;
35
36
37
38
39
40
41 class ApiExceptionHandlerAutoConfigurationTest {
42
43 private ApiExceptionHandlerAutoConfiguration target;
44
45
46
47
48 @BeforeEach
49 void setup() {
50 RestApiExceptionMapperBootProperties properties = new RestApiExceptionMapperBootProperties();
51 target = new ApiExceptionHandlerAutoConfiguration(properties);
52 }
53
54
55
56
57 @Test
58 void init() {
59 assertThatNoException().isThrownBy(() -> target.init());
60 }
61
62
63
64
65 @Test
66 void apiExceptionHandler() {
67
68 ObjectProvider<ErrorAttributes> errorAttributes = mock(ObjectProvider.class);
69 when(errorAttributes.getIfAvailable()).thenReturn(mock(ErrorAttributes.class));
70
71
72 ObjectProvider<WebProperties> webProperties = mock(ObjectProvider.class);
73 when(webProperties.getIfAvailable(any())).thenReturn(new WebProperties());
74
75 ApplicationContext applicationContext = mock(ApplicationContext.class);
76 when(applicationContext.getClassLoader())
77 .thenReturn(ApplicationContext.class.getClassLoader());
78
79
80 ObjectProvider<ServerCodecConfigurer> serverCodecConfigurer = mock(ObjectProvider.class);
81 when(serverCodecConfigurer.getIfAvailable()).thenReturn(mock(ServerCodecConfigurer.class));
82
83
84 ObjectProvider<RestApiExceptionMapper> restApiExceptionMapper = mock(ObjectProvider.class);
85 when(restApiExceptionMapper.getIfAvailable()).thenReturn(new RestApiExceptionMapperForWebFlux(
86 new RestApiExceptionMapperBootProperties().toRestApiExceptionMapperProperties(),
87 "test"
88 ));
89
90 ApiExceptionHandler actual = target.apiExceptionHandler(
91 errorAttributes,
92 webProperties,
93 applicationContext,
94 serverCodecConfigurer,
95 restApiExceptionMapper);
96
97 assertThat(actual)
98 .isNotNull();
99 }
100 }