23
23
24
24
import org .junit .jupiter .api .Test ;
25
25
import org .springframework .boot .test .context .SpringBootTest ;
26
+ import org .springframework .boot .test .context .TestConfiguration ;
27
+ import org .springframework .context .annotation .Bean ;
26
28
import org .springframework .test .annotation .DirtiesContext ;
27
29
import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
28
30
29
31
import io .grpc .Channel ;
32
+ import io .grpc .stub .AbstractStub ;
30
33
import net .devh .boot .grpc .client .inject .GrpcClient ;
34
+ import net .devh .boot .grpc .client .stubfactory .StandardJavaGrpcStubFactory ;
35
+ import net .devh .boot .grpc .client .stubfactory .StubFactory ;
31
36
import net .devh .boot .grpc .test .config .BaseAutoConfiguration ;
32
37
import net .devh .boot .grpc .test .config .InProcessConfiguration ;
33
38
import net .devh .boot .grpc .test .config .ServiceConfiguration ;
39
+ import net .devh .boot .grpc .test .inject .CustomGrpc .ConstructorAccessibleStub ;
40
+ import net .devh .boot .grpc .test .inject .CustomGrpc .CustomAccessibleStub ;
41
+ import net .devh .boot .grpc .test .inject .CustomGrpc .FactoryMethodAccessibleStub ;
42
+ import net .devh .boot .grpc .test .inject .GrpcClientInjectionTest .TestConfig ;
34
43
import net .devh .boot .grpc .test .proto .TestServiceGrpc .TestServiceBlockingStub ;
35
44
import net .devh .boot .grpc .test .proto .TestServiceGrpc .TestServiceFutureStub ;
36
45
import net .devh .boot .grpc .test .proto .TestServiceGrpc .TestServiceStub ;
41
50
* @author Daniel Theuke ([email protected] )
42
51
*/
43
52
@ SpringBootTest
44
- @ SpringJUnitConfig (classes = {InProcessConfiguration .class , ServiceConfiguration .class , BaseAutoConfiguration .class })
53
+ @ SpringJUnitConfig (classes = {TestConfig .class , InProcessConfiguration .class , ServiceConfiguration .class ,
54
+ BaseAutoConfiguration .class })
45
55
@ DirtiesContext
46
- public class GrpcClientInjectionTest {
56
+ class GrpcClientInjectionTest {
47
57
48
58
@ GrpcClient ("test" )
49
- protected Channel channel ;
59
+ Channel channel ;
50
60
@ GrpcClient ("test" )
51
- protected TestServiceStub stub ;
61
+ TestServiceStub stub ;
52
62
@ GrpcClient ("test" )
53
- protected TestServiceBlockingStub blockingStub ;
63
+ TestServiceBlockingStub blockingStub ;
54
64
@ GrpcClient ("test" )
55
- protected TestServiceFutureStub futureStub ;
56
-
57
- protected Channel channelSetted ;
58
- protected TestServiceStub stubSetted ;
59
- protected TestServiceBlockingStub blockingStubSetted ;
60
- protected TestServiceFutureStub futureStubSetted ;
65
+ TestServiceFutureStub futureStub ;
66
+ @ GrpcClient ("test" )
67
+ ConstructorAccessibleStub constructorStub ;
68
+ @ GrpcClient ("test" )
69
+ FactoryMethodAccessibleStub factoryMethodStub ;
70
+ @ GrpcClient ("test" )
71
+ CustomAccessibleStub customStub ;
61
72
73
+ Channel channelSetted ;
74
+ TestServiceStub stubSetted ;
75
+ TestServiceBlockingStub blockingStubSetted ;
76
+ TestServiceFutureStub futureStubSetted ;
77
+ ConstructorAccessibleStub constructorStubSetted ;
78
+ FactoryMethodAccessibleStub factoryMethodStubSetted ;
79
+ CustomAccessibleStub customStubSetted ;
62
80
63
81
@ PostConstruct
64
82
public void init () {
@@ -67,44 +85,93 @@ public void init() {
67
85
assertNotNull (this .stub , "stub" );
68
86
assertNotNull (this .blockingStub , "blockingStub" );
69
87
assertNotNull (this .futureStub , "futureStub" );
88
+ assertNotNull (this .constructorStub , "constructorStub" );
89
+ assertNotNull (this .factoryMethodStub , "factoryMethodStub" );
90
+ assertNotNull (this .customStub , "customStub" );
70
91
}
71
92
72
93
@ GrpcClient ("test" )
73
- public void inject (Channel channel ) {
94
+ void inject (final Channel channel ) {
74
95
assertNotNull (channel , "channel" );
75
96
this .channelSetted = channel ;
76
97
}
77
98
78
99
@ GrpcClient ("test" )
79
- public void inject (TestServiceStub stub ) {
100
+ void inject (final TestServiceStub stub ) {
80
101
assertNotNull (stub , "stub" );
81
102
this .stubSetted = stub ;
82
103
}
83
104
84
105
@ GrpcClient ("test" )
85
- public void inject (TestServiceBlockingStub stub ) {
106
+ void inject (final TestServiceBlockingStub stub ) {
86
107
assertNotNull (stub , "stub" );
87
108
this .blockingStubSetted = stub ;
88
109
}
89
110
90
111
@ GrpcClient ("test" )
91
- public void inject (TestServiceFutureStub stub ) {
112
+ void inject (final TestServiceFutureStub stub ) {
92
113
assertNotNull (stub , "stub" );
93
114
this .futureStubSetted = stub ;
94
115
}
95
116
117
+ @ GrpcClient ("test" )
118
+ void inject (final ConstructorAccessibleStub stub ) {
119
+ assertNotNull (stub , "stub" );
120
+ this .constructorStubSetted = stub ;
121
+ }
122
+
123
+ @ GrpcClient ("test" )
124
+ void inject (final FactoryMethodAccessibleStub stub ) {
125
+ assertNotNull (stub , "stub" );
126
+ this .factoryMethodStubSetted = stub ;
127
+ }
128
+
129
+ @ GrpcClient ("test" )
130
+ void inject (final CustomAccessibleStub stub ) {
131
+ assertNotNull (stub , "stub" );
132
+ this .customStubSetted = stub ;
133
+ }
134
+
96
135
@ Test
97
- public void testAllSet () {
136
+ void testAllSet () {
98
137
// Field injection
99
138
assertNotNull (this .channel , "channel" );
100
139
assertNotNull (this .stub , "stub" );
101
140
assertNotNull (this .blockingStub , "blockingStub" );
102
141
assertNotNull (this .futureStub , "futureStub" );
142
+ assertNotNull (this .constructorStub , "constructorStub" );
143
+ assertNotNull (this .factoryMethodStub , "factoryMethodStub" );
144
+ assertNotNull (this .customStub , "customStub" );
103
145
// Setter injection
104
146
assertNotNull (this .channelSetted , "channelSetted" );
105
147
assertNotNull (this .stubSetted , "stubSetted" );
106
148
assertNotNull (this .blockingStubSetted , "blockingStubSetted" );
107
149
assertNotNull (this .futureStubSetted , "futureStubSetted" );
150
+ assertNotNull (this .constructorStubSetted , "constructorStubSetted" );
151
+ assertNotNull (this .factoryMethodStubSetted , "factoryMethodStubSetted" );
152
+ assertNotNull (this .customStubSetted , "customStubSetted" );
153
+ }
154
+
155
+ @ TestConfiguration
156
+ public static class TestConfig {
157
+
158
+ @ Bean
159
+ StubFactory customStubFactory () {
160
+ return new StandardJavaGrpcStubFactory () {
161
+
162
+ @ Override
163
+ public boolean isApplicable (final Class <? extends AbstractStub <?>> stubType ) {
164
+ return CustomStub .class .isAssignableFrom (stubType );
165
+ }
166
+
167
+ @ Override
168
+ protected String getFactoryMethodName () {
169
+ return "custom" ;
170
+ }
171
+
172
+ };
173
+ }
174
+
108
175
}
109
176
110
177
}
0 commit comments