Spring Boot 单元测试之 @WebIntegrationTest

本文介绍如何使用SpringBoot进行Web应用程序的集成测试,包括使用@WebIntegrationTest注解启动应用并监听正常端口,以便通过HTTP进行测试。同时展示了如何在测试中注入组件及使用RestTemplate与运行中的服务器交互。

Spring Boot官方文档 ----35.3章节 ,原文如下

The context loader guesses whether you want to test a web application or not (e.g.
with MockMVC) by looking for the @WebIntegrationTest or @WebAppConfiguration
annotations. (MockMVC and @WebAppConfiguration are part of spring-test).
If you want a web application to start up and listen on its normal port, so you can test it with
HTTP (e.g. using RestTemplate), annotate your test class (or one of its superclasses) with
@WebIntegrationTest. This can be very useful because it means you can test the full stack of your
application, but also inject its components into the test class and use them to assert the internal state
of the application after an HTTP interaction. For example:

[java]  view plain  copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)  
  3. @WebIntegrationTest  
  4. public class CityRepositoryIntegrationTests {  
  5. @Autowired  
  6. CityRepository repository;  
  7. RestTemplate restTemplate = new TestRestTemplate();  
  8. // ... interact with the running server  
  9. }  
Note
Spring’s test framework will cache application contexts between tests. Therefore, as long as your
tests share the same configuration, the time consuming process of starting and stopping the server
will only happen once, regardless of the number of tests that actually run.
To change the port you can add environment properties to @WebIntegrationTest as colonor
equals-separated name-value pairs, e.g. @WebIntegrationTest("server.port:9000").
Additionally you can set the server.port and management.port properties to 0 in order to run your
integration tests using random ports. For example:
[java]  view plain  copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @SpringApplicationConfiguration(classes = MyApplication.class)  
  3. @WebIntegrationTest({"server.port=0""management.port=0"})  
  4. public class SomeIntegrationTests {  
  5. // ...  
  6. }  
See Section 64.5, “Discover the HTTP port at runtime” for a description of how you can discover the
actual port that was allocated for the duration of the tests.


Spring上下文加载器是通过@WebIntegrationTest 或者 @WebAppConfiguration注解来判断是否要测试一个web应用程序。如果你要启动一个web应用程序而且让其端口正常监听,那就可以使用HTTP测试(添加注解@WebIntegrationTest)来实现,这样就意味着你可以测试整个完整的程序,但是也会把组件注入到测试类中,在HTTP交互之后使用组件来维护程序内部的状态,例如:

[java]  view plain  copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)  
  3. @WebIntegrationTest  
  4. public class CityRepositoryIntegrationTests {  
  5. @Autowired  
  6. CityRepository repository;  
  7. RestTemplate restTemplate = new TestRestTemplate();  
  8. // ... interact with the running server  
  9. }  

Spring的测试框架会测试中缓存应用程序上下文,所以只要你的测试使用了同一个配置信息,不管实际的测试次数有多少,服务器的关闭和启动时时间消耗的进程都只发生一次。另外,你还可以在@WebIntegrationTest注解上添加环境配置实现端口的改动,

e.g. @WebIntegrationTest("server.port:9000").

还可以设置server.port  management.port配置来实现你的整合测试使用随机的端口号。

例如:

[java]  view plain  copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @SpringApplicationConfiguration(classes = MyApplication.class)  
  3. @WebIntegrationTest({"server.port=0""management.port=0"})  
  4. public class SomeIntegrationTests {  
  5. // ...  
  6. }  
今天在测试的时候遇到一个问题,测试一个Web应用程序时,

 @Autowired

private WebApplicationContext wac;

这个注解无法识别,报错显示没有确定的bean,发现少加了@WebIntegrationTest注解,让Spring识别我正在测试一个web应用程序,所以会报错,加上这个注解就可以了。


刚刚接触Spring Boot,要多看官方的文档,了解原理,多看一些例子

参考:Spring Boot官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值