Skip to content

Commit 89004b0

Browse files
committed
Add check for Configuration or TestConfiguration presence, add test
1 parent fae40e7 commit 89004b0

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

grpc-client-spring-boot-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515

1616
api project(':grpc-common-spring-boot')
1717
api 'org.springframework.boot:spring-boot-starter'
18+
api 'org.springframework.boot:spring-boot-starter-test'
1819
optionalSupportImplementation 'org.springframework.boot:spring-boot-starter-actuator'
1920
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
2021
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientBeanPostProcessor.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
import org.springframework.beans.factory.BeanDefinitionStoreException;
3434
import org.springframework.beans.factory.config.BeanPostProcessor;
3535
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
36+
import org.springframework.boot.test.context.TestConfiguration;
3637
import org.springframework.context.ApplicationContext;
3738
import org.springframework.context.ConfigurableApplicationContext;
39+
import org.springframework.context.annotation.Configuration;
3840
import org.springframework.core.annotation.AnnotationUtils;
3941
import org.springframework.util.ReflectionUtils;
4042

@@ -105,16 +107,20 @@ public Object postProcessBeforeInitialization(final Object bean, final String be
105107
}
106108
}
107109

108-
for (final GrpcClientBean beanClientIterator : clazz.getAnnotationsByType(GrpcClientBean.class)) {
109-
final String beanNameToCreate = getBeanName(beanClientIterator);
110-
try {
111-
final ConfigurableListableBeanFactory beanFactory = getConfigurableBeanFactory();
112-
final Object beanValue =
113-
processInjectionPoint(null, beanClientIterator.clazz(), beanClientIterator.client());
114-
beanFactory.registerSingleton(beanNameToCreate, beanValue);
115-
} catch (final Exception e) {
116-
throw new BeanCreationException(
117-
"Could not create and register grpc client bean: {}", beanNameToCreate, e);
110+
if (isAnnotatedWithConfiguration(clazz)) {
111+
for (final GrpcClientBean beanClientIterator : clazz.getAnnotationsByType(GrpcClientBean.class)) {
112+
113+
final String beanNameToCreate = getBeanName(beanClientIterator);
114+
try {
115+
final ConfigurableListableBeanFactory beanFactory = getConfigurableBeanFactory();
116+
final Object beanValue =
117+
processInjectionPoint(null, beanClientIterator.clazz(), beanClientIterator.client());
118+
beanFactory.registerSingleton(beanNameToCreate, beanValue);
119+
} catch (final Exception e) {
120+
throw new BeanCreationException(
121+
"Could not create and register grpc client bean: {} from class {}", beanNameToCreate,
122+
clazz.getSimpleName(), e);
123+
}
118124
}
119125
}
120126

@@ -318,4 +324,16 @@ private String getBeanName(final GrpcClientBean grpcClientBean) {
318324
}
319325
}
320326

327+
/**
328+
* The method is used to check for the presence of an annotation {@link Configuration} or {@link TestConfiguration}
329+
*
330+
* @param clazz instance of the class
331+
* @return does the class have an annotation or not
332+
*/
333+
private boolean isAnnotatedWithConfiguration(final Class<?> clazz) {
334+
final Configuration[] configurationAnnotation = clazz.getAnnotationsByType(Configuration.class);
335+
final TestConfiguration[] testConfigurationAnnotation = clazz.getAnnotationsByType(TestConfiguration.class);
336+
return configurationAnnotation.length != 0 || testConfigurationAnnotation.length != 0;
337+
}
338+
321339
}

tests/src/test/java/net/devh/boot/grpc/test/inject/GrpcClientBeanInjectionTest.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.boot.test.context.TestConfiguration;
2727
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
2829
import org.springframework.test.annotation.DirtiesContext;
2930
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3031

@@ -48,6 +49,7 @@
4849
@SpringJUnitConfig(
4950
classes = {
5051
GrpcClientBeanInjectionTest.TestConfig.class,
52+
GrpcClientBeanInjectionTest.CustomConfig.class,
5153
InProcessConfiguration.class,
5254
ServiceConfiguration.class,
5355
BaseAutoConfiguration.class
@@ -73,36 +75,67 @@ public class GrpcClientBeanInjectionTest {
7375
@Autowired
7476
String aboutMethodInjectedBlockingStubBean;
7577

78+
@Autowired
79+
TestServiceGrpc.TestServiceBlockingStub stubFromSpringConfiguration;
80+
81+
/**
82+
* Test should cover bean simple single bean creation with {@link GrpcClientBean} annotation with
83+
* {@link TestConfiguration}
84+
*/
7685
@Test
77-
void singleContextInjectionTest() {
86+
void singleContextInjectionFromTestConfigurationTest() {
7887
assertNotNull(blockingStub, "blockingStub");
7988
}
8089

90+
/**
91+
* Test should cover bean simple single bean creation with {@link GrpcClientBean} annotation with
92+
* {@link Configuration}
93+
*/
94+
@Test
95+
void singleContextInjectionFromConfigurationTest() {
96+
assertNotNull(stubFromSpringConfiguration, "stubFromSpringConfiguration");
97+
}
98+
99+
/**
100+
* Test should cover creation of another bean with different stub class and same grpc client definition
101+
*/
81102
@Test
82103
void anotherSubTypeAndSameClientDefinitionTest() {
83104
assertNotNull(futureStubForClientTest, "futureStubForClientTest");
84105
}
85106

107+
/**
108+
* Test should cover creation of another bean same different stub class, but different grpc client definition
109+
*/
86110
@Test
87111
void twoDifferentClientDefinitionsTest() {
88112
assertNotNull(anotherBlockingStub, "blockingStub");
89113
}
90114

115+
/**
116+
* Test should cover creation of another bean with different service and stub class with same grpc client definition
117+
*/
118+
@Test
119+
void anotherGrpcServiceAndSameGrpcClientDefinitionTest() {
120+
assertNotNull(anotherServiceClientBean, "anotherServiceClientBean");
121+
}
122+
123+
/**
124+
* Test should cover creation of bean without defined bean name
125+
*/
91126
@Test
92127
void unnamedBeanContextInjectionTest() {
93128
assertNotNull(unnamedTestServiceBlockingStub, "unnamedTestServiceBlockingStub");
94129
}
95130

131+
/**
132+
* Test should cover bean method injection via {@link Autowired} and {@link Qualifier} from context
133+
*/
96134
@Test
97135
void autoWiringQualifierMethodInjectionFromContextTest() {
98136
assertNotNull(aboutMethodInjectedBlockingStubBean, "aboutBlockingStubBean");
99137
}
100138

101-
@Test
102-
void anotherGrpcServiceAndSameGrpcClientDefinitionTest() {
103-
assertNotNull(anotherServiceClientBean, "anotherServiceClientBean");
104-
}
105-
106139
@TestConfiguration
107140
@GrpcClientBeans(value = {
108141
@GrpcClientBean(
@@ -151,4 +184,12 @@ protected String getFactoryMethodName() {
151184
};
152185
}
153186
}
187+
188+
@Configuration
189+
@GrpcClientBean(
190+
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
191+
beanName = "stubFromSpringConfiguration",
192+
client = @GrpcClient("test2"))
193+
public static class CustomConfig {
194+
}
154195
}

0 commit comments

Comments
 (0)