Skip to content

Commit ab2f005

Browse files
Dave Syerdsyer
Dave Syer
authored andcommitted
Add a test for autroconfig of servlet
Signed-off-by: Dave Syer <dsyer@vmware.com>
1 parent c358f6e commit ab2f005

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

spring-grpc-spring-boot-autoconfigure/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
<version>${project.version}</version>
4646
<optional>true</optional>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-web</artifactId>
51+
<optional>true</optional>
52+
</dependency>
4853
<dependency>
4954
<groupId>org.apache.tomcat.embed</groupId>
5055
<artifactId>tomcat-embed-core</artifactId>
@@ -86,4 +91,4 @@
8691

8792
</dependencies>
8893

89-
</project>
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2023-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.grpc.autoconfigure.server;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyInt;
22+
import static org.mockito.Mockito.inOrder;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
26+
import java.time.Duration;
27+
import java.util.List;
28+
import java.util.concurrent.TimeUnit;
29+
30+
import org.assertj.core.api.InstanceOfAssertFactories;
31+
import org.junit.jupiter.api.Test;
32+
import org.mockito.InOrder;
33+
import org.mockito.MockedStatic;
34+
import org.mockito.Mockito;
35+
import org.mockito.stubbing.Answer;
36+
import org.springframework.boot.autoconfigure.AutoConfigurations;
37+
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
38+
import org.springframework.boot.test.context.FilteredClassLoader;
39+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
40+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
41+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
42+
import org.springframework.context.annotation.Bean;
43+
import org.springframework.context.annotation.Configuration;
44+
import org.springframework.core.annotation.Order;
45+
import org.springframework.grpc.server.GrpcServerFactory;
46+
import org.springframework.grpc.server.NettyGrpcServerFactory;
47+
import org.springframework.grpc.server.ServerBuilderCustomizer;
48+
import org.springframework.grpc.server.ShadedNettyGrpcServerFactory;
49+
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;
50+
51+
import io.grpc.BindableService;
52+
import io.grpc.Grpc;
53+
import io.grpc.ServerBuilder;
54+
import io.grpc.ServerServiceDefinition;
55+
import io.grpc.ServiceDescriptor;
56+
import io.grpc.netty.NettyServerBuilder;
57+
58+
/**
59+
* Tests for {@link GrpcServerAutoConfiguration}.
60+
*
61+
* @author Chris Bono
62+
*/
63+
class GrpcServletAutoConfigurationTests {
64+
65+
private WebApplicationContextRunner contextRunner() {
66+
BindableService service = mock();
67+
ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build();
68+
when(service.bindService()).thenReturn(serviceDefinition);
69+
// NOTE: we use noop server lifecycle to avoid startup
70+
return new WebApplicationContextRunner()
71+
.withConfiguration(
72+
AutoConfigurations.of(GrpcServerAutoConfiguration.class, GrpcServerFactoryAutoConfiguration.class))
73+
.withBean(BindableService.class, () -> service);
74+
}
75+
76+
@Test
77+
void whenGrpcNotOnClasspathAutoConfigurationIsSkipped() {
78+
this.contextRunner()
79+
.withClassLoader(new FilteredClassLoader(BindableService.class))
80+
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class)
81+
.doesNotHaveBean(ServletRegistrationBean.class));
82+
}
83+
84+
@Test
85+
void whenNoBindableServicesRegisteredAutoConfigurationIsSkipped() {
86+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class))
87+
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class)
88+
.doesNotHaveBean(ServletRegistrationBean.class));
89+
}
90+
91+
@Test
92+
void whenWebApplicationServletIsAutoConfigured() {
93+
this.contextRunner().run((context) -> assertThat(context).getBean(ServletRegistrationBean.class).isNotNull());
94+
}
95+
96+
}

0 commit comments

Comments
 (0)