Skip to content

Commit b090dbb

Browse files
authored
Update 40.3 Testing Spring Boot applications.md
1 parent 05881cb commit b090dbb

File tree

1 file changed

+13
-43
lines changed

1 file changed

+13
-43
lines changed

IV. Spring Boot features/40.3 Testing Spring Boot applications.md

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,16 @@
22

33
Spring Boot应用只是一个Spring `ApplicationContext`,所以在测试它只需要像处理普通Spring context那样即可。唯一需要注意的是,如果你使用`SpringApplication`创建上下文,外部配置,日志和Spring Boot的其他特性只会在默认的上下文中起作用。
44

5-
Spring Boot提供一个`@SpringApplicationConfiguration`注解用来替换标准的`spring-test``@ContextConfiguration`注解。
6-
7-
8-
如果使用@SpringApplicationConfiguration来设置你的测试中使用的ApplicationContext,它最终将通过SpringApplication创建,并且你将获取到Spring Boot的其他特性。
9-
10-
示例如下:
11-
```java
12-
@RunWith(SpringJUnit4ClassRunner.class)
13-
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
14-
public class CityRepositoryIntegrationTests {
15-
@Autowired
16-
CityRepository repository;
17-
// ...
18-
}
19-
```
20-
**提示**:上下文加载器会通过查找@WebIntegrationTest@WebAppConfiguration注解来猜测你想测试的是否是web应用(例如,是否使用MockMVC,MockMVC和@WebAppConfiguration是spring-test的一部分)。
21-
22-
如果想让一个web应用启动,并监听它的正常的端口,你可以使用HTTP来测试它(比如,使用RestTemplate),并使用@WebIntegrationTest注解你的测试类(或它的一个父类)。这很有用,因为它意味着你可以对你的应用进行全栈测试,但在一次HTTP交互后,你需要在你的测试类中注入相应的组件并使用它们断言应用的内部状态。
23-
24-
示例:
25-
```java
26-
@RunWith(SpringJUnit4ClassRunner.class)
27-
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
28-
@WebIntegrationTest
29-
public class CityRepositoryIntegrationTests {
30-
@Autowired
31-
CityRepository repository;
32-
RestTemplate restTemplate = new TestRestTemplate();
33-
// ... interact with the running server
34-
}
35-
```
36-
****:Spring测试框架在每次测试时会缓存应用上下文。因此,只要你的测试共享相同的配置,不管你实际运行多少测试,开启和停止服务器只会发生一次。
37-
38-
你可以为@WebIntegrationTest添加环境变量属性来改变应用服务器端口号,比如@WebIntegrationTest("server.port:9000")。此外,你可以将server.port和management.port属性设置为0来让你的集成测试使用随机的端口号,例如:
39-
```java
40-
@RunWith(SpringJUnit4ClassRunner.class)
41-
@SpringApplicationConfiguration(classes = MyApplication.class)
42-
@WebIntegrationTest({"server.port=0", "management.port=0"})
43-
public class SomeIntegrationTests {
44-
// ...
45-
}
46-
```
47-
可以查看[Section 64.4, “Discover the HTTP port at runtime”](../IX. ‘How-to’ guides/64.4. Discover the HTTP port at runtime.md),它描述了如何在测试期间发现分配的实际端口。
5+
Spring Boot提供一个`@SpringApplicationConfiguration`注解用于替换标准的`spring-test` `@ContextConfiguration`注解,该组件工作方式是通过`SpringApplication`创建用于测试的`ApplicationContext`
6+
7+
你可以使用`@SpringBootTest``webEnvironment`属性定义怎么运行测试:
8+
9+
* `MOCK` - 加载`WebApplicationContext`,并提供一个mock servlet环境,使用该注解时内嵌servlet容器将不会启动。如果classpath下不存在servlet APIs,该模式将创建一个常规的non-web `ApplicationContext`
10+
11+
* `RANDOM_PORT` - 加载`EmbeddedWebApplicationContext`,并提供一个真实的servlet环境。使用该模式内嵌容器将启动,并监听在一个随机端口。
12+
13+
* `DEFINED_PORT` - 加载`EmbeddedWebApplicationContext`,并提供一个真实的servlet环境。使用该模式内嵌容器将启动,并监听一个定义好的端口(比如`application.properties`中定义的或默认的`8080`端口)。
14+
15+
* `NONE` - 使用`SpringApplication`加载一个`ApplicationContext`,但不提供任何servlet环境(不管是mock还是其他)。
16+
17+
**** 不要忘记在测试用例上添加`@RunWith(SpringRunner.class)`,否则该注解将被忽略。

0 commit comments

Comments
 (0)