Skip to content

Commit 42cbea3

Browse files
committed
Add some notes on @AutoConfigureInProcessTransport
Fixes #285
1 parent ad4ce7e commit 42cbea3

File tree

1 file changed

+34
-1
lines changed
  • spring-grpc-docs/src/main/antora/modules/ROOT/pages

1 file changed

+34
-1
lines changed

spring-grpc-docs/src/main/antora/modules/ROOT/pages/server.adoc

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,40 @@ If you include `spring-grpc-test` in your project, your gRPC server in a `@Sprin
241241
All clients that connect to any server via the autoconfigured `GrpcChannelFactory` will be able to connect to it.
242242
You can switch the in-process server on by setting `spring.grpc.test.inprocess.enabled` to `true` or by adding the `@AutoConfigureInProcessTransport` annotation to your `@SpringBootTest` class.
243243

244-
NOTE: When the in-process server is run in test mode (as opposed to <<in-process-server,running normally>>) it replaces the regular server and channel factories (e.g. Netty)
244+
The in-process transport is an opt-in feature, so it requires an explicit configuration.
245+
You can switch it on by setting `spring.grpc.test.inprocess.enabled` to `true` or by adding the `@AutoConfigureInProcessTransport` annotation to your `@SpringBootTest` class.
246+
There is no need to set `spring.grpc.server.inprocess.name` as that is done automatically.
247+
Using the annotation is equivalent to setting the "enabled" property to true, and in addition marking the in-process transport as "exclusive", meaning that no other transport will be used.
248+
249+
The `@AutoConfigureInProcessTransport` annotation works even if the test is not a `@SpringBootTest`, so if you only want to test the gRPC server layer you can use `@SpringJUnitConfig` with `@EnableAutoconfiguration` and add the `BindableService` beans that you want to test, either manually or as a `@ComponentScan`.
250+
Here's an example from the samples:
251+
252+
```java
253+
@TestPropertySource(properties = { "spring.grpc.client.default-channel.address=localhost:9090" })
254+
@SpringJUnitConfig(TestConfig.class)
255+
@AutoConfigureInProcessTransport
256+
public class GrpcServerSideTests {
257+
258+
@Autowired
259+
private SimpleBlockingStub stub;
260+
261+
@Test
262+
void contextLoads() {
263+
// Test the service using the stub...
264+
}
265+
266+
@TestConfiguration
267+
@Import({ GrpcServerService.class })
268+
@EnableAutoConfiguration
269+
static class TestConfig {
270+
271+
}
272+
273+
}
274+
```
275+
276+
NOTE: When the in-process server is run in test mode (as opposed to <<in-process-server,running normally>>) it replaces the regular server and channel factories (e.g. Netty).
277+
All channel target addresses are magically replaced with the in-process server name, so that clients can connect to it without any special configuration (hence the "default-channel" configuration in the example above, which is there purely to trigger automatic stub creation).
245278

246279

247280
== Security

0 commit comments

Comments
 (0)