|
| 1 | +package org.springframework.grpc.sample; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Nested; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.boot.test.context.TestConfiguration; |
| 10 | +import org.springframework.context.ApplicationContext; |
| 11 | +import org.springframework.grpc.client.FutureStubFactory; |
| 12 | +import org.springframework.grpc.client.ImportGrpcClients; |
| 13 | +import org.springframework.grpc.sample.proto.SimpleGrpc; |
| 14 | +import org.springframework.grpc.test.AutoConfigureInProcessTransport; |
| 15 | + |
| 16 | +import io.grpc.stub.AbstractStub; |
| 17 | + |
| 18 | +public class GrpcClientApplicationTests { |
| 19 | + |
| 20 | + @Nested |
| 21 | + @SpringBootTest |
| 22 | + @AutoConfigureInProcessTransport |
| 23 | + class NoAutowiredClients { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private ApplicationContext context; |
| 27 | + |
| 28 | + @Test |
| 29 | + void noStubIsCreated() { |
| 30 | + assertThat(context.containsBeanDefinition("simpleBlockingStub")).isFalse(); |
| 31 | + assertThat(context.containsBeanDefinition("simpleStub")).isFalse(); |
| 32 | + assertThat(context.containsBeanDefinition("simpleFutureStub")).isFalse(); |
| 33 | + assertThat(context.getBeanNamesForType(AbstractStub.class)).isEmpty(); |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @Nested |
| 39 | + @SpringBootTest(properties = "spring.grpc.client.default-channel.address=0.0.0.0:9090") |
| 40 | + @AutoConfigureInProcessTransport |
| 41 | + class DefaultAutowiredClients { |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private ApplicationContext context; |
| 45 | + |
| 46 | + @Test |
| 47 | + void onlyDefaultStubIsCreated() { |
| 48 | + assertThat(context.containsBeanDefinition("simpleBlockingStub")).isTrue(); |
| 49 | + assertThat(context.getBean(SimpleGrpc.SimpleBlockingStub.class)).isNotNull(); |
| 50 | + assertThat(context.containsBeanDefinition("simpleStub")).isFalse(); |
| 51 | + assertThat(context.containsBeanDefinition("simpleFutureStub")).isFalse(); |
| 52 | + assertThat(context.getBeanNamesForType(AbstractStub.class)).hasSize(1); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Nested |
| 58 | + @SpringBootTest(properties = "spring.grpc.client.default-channel.address=0.0.0.0:9090") |
| 59 | + @AutoConfigureInProcessTransport |
| 60 | + class SpecificAutowiredClients { |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private ApplicationContext context; |
| 64 | + |
| 65 | + @Test |
| 66 | + void stubOfCorrectTypeIsCreated() { |
| 67 | + assertThat(context.containsBeanDefinition("simpleFutureStub")).isTrue(); |
| 68 | + assertThat(context.getBean(SimpleGrpc.SimpleFutureStub.class)).isNotNull(); |
| 69 | + assertThat(context.containsBeanDefinition("simpleStub")).isFalse(); |
| 70 | + assertThat(context.containsBeanDefinition("simpleBlockingStub")).isFalse(); |
| 71 | + assertThat(context.getBeanNamesForType(AbstractStub.class)).hasSize(1); |
| 72 | + } |
| 73 | + |
| 74 | + @TestConfiguration |
| 75 | + @ImportGrpcClients(basePackageClasses = SimpleGrpc.class, factory = FutureStubFactory.class) |
| 76 | + static class TestConfig { |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments