Skip to content

Commit 340fe8f

Browse files
committed
Add test for cases with @GrpcClientBean and @GrpcClient usage. Fix exception message
1 parent 88d1a3a commit 340fe8f

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ public Object postProcessBeforeInitialization(final Object bean, final String be
117117
beanFactory.registerSingleton(beanNameToCreate, beanValue);
118118
} catch (final Exception e) {
119119
throw new BeanCreationException(
120-
"Could not create and register grpc client bean: {} from class {}", beanNameToCreate,
121-
clazz.getSimpleName(), e);
120+
"Could not create and register grpc client bean " + beanNameToCreate + " from class " +
121+
clazz.getSimpleName(),
122+
e);
122123
}
123124
}
124125
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2016-2021 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.test.inject;
19+
20+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.springframework.beans.factory.BeanCreationException;
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.test.context.TestConfiguration;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.test.annotation.DirtiesContext;
29+
30+
import lombok.extern.slf4j.Slf4j;
31+
import net.devh.boot.grpc.client.inject.GrpcClient;
32+
import net.devh.boot.grpc.client.inject.GrpcClientBean;
33+
import net.devh.boot.grpc.client.inject.GrpcClientBeans;
34+
import net.devh.boot.grpc.test.config.BaseAutoConfiguration;
35+
import net.devh.boot.grpc.test.config.InProcessConfiguration;
36+
import net.devh.boot.grpc.test.config.ServiceConfiguration;
37+
import net.devh.boot.grpc.test.proto.TestServiceGrpc;
38+
39+
/**
40+
* Test case should cover probable negative situations with @GrpcBeanClient and @GrpcClient usage
41+
*/
42+
@Slf4j
43+
@DirtiesContext
44+
public class GrpcClientBeanInjectionNegativeTest {
45+
46+
@Test
47+
void twoSameGrpcClientBeansTest() {
48+
final SpringApplication app = new SpringApplication(
49+
TwoSameGrpcClientBeans.class,
50+
InProcessConfiguration.class,
51+
ServiceConfiguration.class,
52+
BaseAutoConfiguration.class);
53+
assertThrows(BeanCreationException.class, app::run);
54+
}
55+
56+
@Test
57+
void grpcClientBeanAndCustomGrpcClientFieldTest() {
58+
final SpringApplication app = new SpringApplication(
59+
GrpcClientBeanWithCustomGrpcClientFieldConfig.class,
60+
InProcessConfiguration.class,
61+
ServiceConfiguration.class,
62+
BaseAutoConfiguration.class);
63+
assertDoesNotThrow(() -> SpringApplication.exit(app.run()));
64+
}
65+
66+
@TestConfiguration
67+
@GrpcClientBeans(value = {
68+
@GrpcClientBean(
69+
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
70+
beanName = "blockingStub",
71+
client = @GrpcClient("test")),
72+
@GrpcClientBean(
73+
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
74+
beanName = "blockingStub",
75+
client = @GrpcClient("test")),
76+
})
77+
public static class TwoSameGrpcClientBeans {
78+
}
79+
80+
@TestConfiguration
81+
@GrpcClientBean(
82+
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
83+
beanName = "blockingStub",
84+
client = @GrpcClient("testGrpcBeanClient"))
85+
public static class GrpcClientBeanWithCustomGrpcClientFieldConfig {
86+
87+
@GrpcClient("testGrpcBeanClient")
88+
TestServiceGrpc.TestServiceBlockingStub stub;
89+
90+
@Bean
91+
public TestServiceGrpc.TestServiceBlockingStub blockingStub() {
92+
return stub;
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)