11package org .lognet .springboot .grpc .recovery ;
22
3+ import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .Matchers .is ;
5+ import static org .hamcrest .Matchers .isA ;
6+ import static org .hamcrest .Matchers .notNullValue ;
7+ import static org .junit .jupiter .api .Assertions .assertThrows ;
8+ import static org .mockito .ArgumentMatchers .any ;
9+ import static org .mockito .Mockito .never ;
10+ import static org .mockito .Mockito .times ;
11+ import static org .springframework .boot .test .context .SpringBootTest .WebEnvironment .NONE ;
12+
13+ import java .util .concurrent .CompletableFuture ;
14+ import java .util .concurrent .ExecutionException ;
15+ import java .util .concurrent .TimeUnit ;
16+ import java .util .concurrent .TimeoutException ;
17+
318import io .grpc .Metadata ;
419import io .grpc .Status ;
520import io .grpc .StatusRuntimeException ;
1530import org .springframework .boot .test .context .SpringBootTest ;
1631import org .springframework .boot .test .context .TestConfiguration ;
1732import org .springframework .boot .test .mock .mockito .SpyBean ;
33+ import org .springframework .context .annotation .Bean ;
1834import org .springframework .context .annotation .Import ;
1935import org .springframework .test .context .ActiveProfiles ;
2036import org .springframework .test .context .junit4 .SpringRunner ;
2137
22- import java .util .concurrent .CompletableFuture ;
23- import java .util .concurrent .ExecutionException ;
24- import java .util .concurrent .TimeUnit ;
25- import java .util .concurrent .TimeoutException ;
26-
27- import static org .hamcrest .MatcherAssert .assertThat ;
28- import static org .hamcrest .Matchers .is ;
29- import static org .hamcrest .Matchers .isA ;
30- import static org .hamcrest .Matchers .notNullValue ;
31- import static org .junit .jupiter .api .Assertions .assertThrows ;
32- import static org .mockito .ArgumentMatchers .any ;
33- import static org .mockito .Mockito .never ;
34- import static org .mockito .Mockito .times ;
35- import static org .springframework .boot .test .context .SpringBootTest .WebEnvironment .NONE ;
36-
3738@ RunWith (SpringRunner .class )
3839@ SpringBootTest (classes = {DemoApp .class }, webEnvironment = NONE )
3940@ ActiveProfiles ({"disable-security" })
@@ -43,6 +44,7 @@ public class GRpcRecoveryTest extends GrpcServerTestBase {
4344 static class CheckedException extends Exception {
4445
4546 }
47+
4648 static class CheckedException1 extends Exception {
4749
4850 }
@@ -59,16 +61,25 @@ static class Exception1 extends RuntimeException {
5961
6062 }
6163
64+ @ GRpcServiceAdvice
65+ static class BeanCustomErrorHandler {
66+
67+ @ GRpcExceptionHandler
68+ public Status handleA (ExceptionA e , GRpcExceptionScope scope ) {
69+ return Status .NOT_FOUND ;
70+ }
71+ }
72+
6273 @ TestConfiguration
6374 static class Cfg {
6475
76+ @ Bean
77+ public BeanCustomErrorHandler otherErrorHandler () {
78+ return new BeanCustomErrorHandler ();
79+ }
6580
6681 @ GRpcServiceAdvice
6782 static class CustomErrorHandler {
68- @ GRpcExceptionHandler
69- public Status handleA (ExceptionA e , GRpcExceptionScope scope ) {
70- return Status .NOT_FOUND ;
71- }
7283
7384 @ GRpcExceptionHandler
7485 public Status handleB (ExceptionB e , GRpcExceptionScope scope ) {
@@ -87,7 +98,6 @@ public Status handle(Exception e, GRpcExceptionScope scope) {
8798 }
8899 }
89100
90-
91101 @ GRpcService
92102 static class CustomService extends CustomServiceGrpc .CustomServiceImplBase {
93103
@@ -150,12 +160,14 @@ public Status handleB(ExceptionB e, GRpcExceptionScope scope) {
150160 @ SpyBean
151161 private Cfg .CustomErrorHandler handler ;
152162
163+ @ SpyBean
164+ private BeanCustomErrorHandler beanErrorHandler ;
165+
153166
154167 @ Test
155168 public void streamingServiceErrorHandlerTest () throws ExecutionException , InterruptedException , TimeoutException {
156169
157170
158-
159171 final CompletableFuture <Throwable > errorFuture = new CompletableFuture <>();
160172 final StreamObserver <Custom .CustomReply > reply = new StreamObserver <Custom .CustomReply >() {
161173
@@ -180,15 +192,12 @@ public void onCompleted() {
180192 requests .onCompleted ();
181193
182194
183-
184-
185-
186195 final Throwable actual = errorFuture .get (20 , TimeUnit .SECONDS );
187196 assertThat (actual , notNullValue ());
188197 assertThat (actual , isA (StatusRuntimeException .class ));
189- assertThat (((StatusRuntimeException )actual ).getStatus (), is (Status .RESOURCE_EXHAUSTED ));
198+ assertThat (((StatusRuntimeException ) actual ).getStatus (), is (Status .RESOURCE_EXHAUSTED ));
190199
191- Mockito .verify (srv ,times (1 )).handle (any (CheckedException1 .class ),any ());
200+ Mockito .verify (srv , times (1 )).handle (any (CheckedException1 .class ), any ());
192201
193202 }
194203
@@ -205,9 +214,8 @@ public void checkedExceptionHandlerTest() {
205214
206215 Mockito .verify (srv , never ()).handleB (any (), any ());
207216
208-
217+ Mockito . verify ( beanErrorHandler , never ()). handleA ( any (), any ());
209218 Mockito .verify (handler , never ()).handle (any (), any ());
210- Mockito .verify (handler , never ()).handleA (any (), any ());
211219 Mockito .verify (handler , times (1 )).handleCheckedException (any (CheckedException .class ), any ());
212220 Mockito .verify (handler , never ()).handleB (any (), any ());
213221
0 commit comments