Skip to content

Commit 9aca507

Browse files
committed
Add test that fails due to ignored inherited method checks
1 parent 91bd65b commit 9aca507

File tree

2 files changed

+105
-9
lines changed

2 files changed

+105
-9
lines changed

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/advice/GrpcAdviceDiscoverer.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ public class GrpcAdviceDiscoverer implements InitializingBean, ApplicationContex
4949
private Set<Class<?>> annotatedClasses;
5050
private Set<Method> annotatedMethods;
5151

52-
5352
@Override
54-
public void afterPropertiesSet() throws Exception {
53+
public void setApplicationContext(final ApplicationContext applicationContext) {
54+
this.applicationContext = applicationContext;
55+
}
5556

57+
@Override
58+
public void afterPropertiesSet() {
5659
annotatedBeans = applicationContext.getBeansWithAnnotation(GrpcAdvice.class);
5760
annotatedBeans.forEach(
5861
(key, value) -> log.debug("Found gRPC advice: " + key + ", class: " + value.getClass().getName()));
@@ -76,13 +79,7 @@ private Set<Method> findAnnotatedMethods() {
7679
.collect(Collectors.toSet());
7780
}
7881

79-
@Override
80-
public void setApplicationContext(final ApplicationContext applicationContext) {
81-
this.applicationContext = applicationContext;
82-
}
83-
8482
boolean isAnnotationPresent() {
85-
8683
return !annotatedClasses.isEmpty() && !annotatedMethods.isEmpty();
8784
}
8885

@@ -96,5 +93,4 @@ public Set<Method> getAnnotatedMethods() {
9693
return annotatedMethods;
9794
}
9895

99-
10096
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
package net.devh.boot.grpc.server.advice;
3+
4+
import static java.util.Collections.singletonMap;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.reset;
8+
import static org.mockito.Mockito.when;
9+
10+
import java.lang.reflect.Method;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.springframework.context.ApplicationContext;
15+
16+
import io.grpc.Status;
17+
18+
/**
19+
* Tests for {@link GrpcAdviceDiscoverer}.
20+
*/
21+
class GrpcAdviceDiscovererTest {
22+
23+
private final ApplicationContext context = mock(ApplicationContext.class);
24+
25+
@BeforeEach
26+
void beforeEach() {
27+
reset(this.context);
28+
}
29+
30+
/**
31+
* Tests that the {@link GrpcAdviceDiscoverer} discovers inherited methods.
32+
*/
33+
@Test
34+
void testDiscoversInheritedMethods() {
35+
when(this.context.getBeansWithAnnotation(GrpcAdvice.class))
36+
.thenReturn(singletonMap("bean", new Extended()));
37+
38+
final GrpcAdviceDiscoverer disco = new GrpcAdviceDiscoverer();
39+
disco.setApplicationContext(this.context);
40+
disco.afterPropertiesSet();
41+
42+
assertThat(disco.getAnnotatedMethods())
43+
.containsExactlyInAnyOrder(
44+
findMethod(Base.class, "handleRuntimeException", RuntimeException.class),
45+
findMethod(Extended.class, "handleIllegalArgument", IllegalArgumentException.class));
46+
}
47+
48+
@Test
49+
void testOverriddenMethods() {
50+
when(this.context.getBeansWithAnnotation(GrpcAdvice.class))
51+
.thenReturn(singletonMap("bean", new Overriden()));
52+
53+
final GrpcAdviceDiscoverer disco = new GrpcAdviceDiscoverer();
54+
disco.setApplicationContext(this.context);
55+
disco.afterPropertiesSet();
56+
57+
assertThat(disco.getAnnotatedMethods())
58+
.containsExactly(findMethod(Overriden.class, "handleRuntimeException", RuntimeException.class));
59+
}
60+
61+
private static Method findMethod(final Class<?> clazz, final String method, final Class<?>... parameters) {
62+
try {
63+
return clazz.getDeclaredMethod(method, parameters);
64+
} catch (NoSuchMethodException | SecurityException e) {
65+
throw new IllegalStateException("Failed to find method", e);
66+
}
67+
}
68+
69+
@GrpcAdvice
70+
private class Base {
71+
72+
@GrpcExceptionHandler
73+
Status handleRuntimeException(final RuntimeException e) {
74+
return Status.INTERNAL;
75+
}
76+
77+
}
78+
79+
@GrpcAdvice
80+
private class Extended extends Base {
81+
82+
@GrpcExceptionHandler
83+
Status handleIllegalArgument(final IllegalArgumentException e) {
84+
return Status.INVALID_ARGUMENT;
85+
}
86+
87+
}
88+
89+
@GrpcAdvice
90+
private class Overriden extends Base {
91+
92+
@Override
93+
@GrpcExceptionHandler
94+
Status handleRuntimeException(final RuntimeException e) {
95+
return Status.INVALID_ARGUMENT;
96+
}
97+
98+
}
99+
100+
}

0 commit comments

Comments
 (0)