Skip to content

Commit 5932fbc

Browse files
author
Alexander Furer
committed
test for not spring bean interceptor
1 parent 20fa970 commit 5932fbc

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

grpc-spring-boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/DemoApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public GreeterService greeterService() {
2525
return new GreeterService();
2626
}
2727

28-
@GRpcService
28+
@GRpcService(interceptors = NotSpringBeanInterceptor.class)
2929
public static class CalculatorService extends CalculatorGrpc.CalculatorImplBase{
3030
@Override
3131
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.lognet.springboot.grpc.demo;
2+
3+
import io.grpc.Metadata;
4+
import io.grpc.ServerCall;
5+
import io.grpc.ServerCallHandler;
6+
import io.grpc.ServerInterceptor;
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
@Slf4j
10+
public class NotSpringBeanInterceptor implements ServerInterceptor {
11+
12+
@Override
13+
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
14+
ServerCallHandler<ReqT, RespT> next) {
15+
System.out.println("I'm not Spring bean interceptor and still being invoked...");
16+
return next.startCall(call, headers);
17+
}
18+
}

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/DemoAppTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import java.util.concurrent.ExecutionException;
2929

30+
import static org.hamcrest.CoreMatchers.containsString;
31+
import static org.hamcrest.CoreMatchers.not;
3032
import static org.junit.Assert.*;
3133
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
3234

@@ -73,8 +75,13 @@ public void interceptorsTest() throws ExecutionException, InterruptedException {
7375

7476

7577
// log interceptor should be invoked only on GreeterService and not CalculatorService
76-
outputCapture.expect(CoreMatchers.containsString(GreeterGrpc.METHOD_SAY_HELLO.getFullMethodName()));
77-
outputCapture.expect(CoreMatchers.not(CoreMatchers.containsString(CalculatorGrpc.METHOD_CALCULATE.getFullMethodName())));
78+
outputCapture.expect(containsString(GreeterGrpc.METHOD_SAY_HELLO.getFullMethodName()));
79+
outputCapture.expect(not(containsString(CalculatorGrpc.METHOD_CALCULATE.getFullMethodName())));
80+
81+
82+
outputCapture.expect(containsString("I'm not Spring bean interceptor and still being invoked..."));
83+
84+
7885

7986
}
8087

0 commit comments

Comments
 (0)