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:
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
- @WebIntegrationTest
- public class CityRepositoryIntegrationTests {
- @Autowired
- CityRepository repository;
- RestTemplate restTemplate = new TestRestTemplate();
- // ... interact with the running server
- }
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:
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = MyApplication.class)
- @WebIntegrationTest({"server.port=0", "management.port=0"})
- public class SomeIntegrationTests {
- // ...
- }
actual port that was allocated for the duration of the tests.
Spring上下文加载器是通过@WebIntegrationTest 或者 @WebAppConfiguration注解来判断是否要测试一个web应用程序。如果你要启动一个web应用程序而且让其端口正常监听,那就可以使用HTTP测试(添加注解@WebIntegrationTest)来实现,这样就意味着你可以测试整个完整的程序,但是也会把组件注入到测试类中,在HTTP交互之后使用组件来维护程序内部的状态,例如:
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
- @WebIntegrationTest
- public class CityRepositoryIntegrationTests {
- @Autowired
- CityRepository repository;
- RestTemplate restTemplate = new TestRestTemplate();
- // ... interact with the running server
- }
Spring的测试框架会测试中缓存应用程序上下文,所以只要你的测试使用了同一个配置信息,不管实际的测试次数有多少,服务器的关闭和启动时时间消耗的进程都只发生一次。另外,你还可以在@WebIntegrationTest注解上添加环境配置实现端口的改动,
e.g. @WebIntegrationTest("server.port:9000").
还可以设置server.port 和 management.port配置来实现你的整合测试使用随机的端口号。
例如:
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = MyApplication.class)
- @WebIntegrationTest({"server.port=0", "management.port=0"})
- public class SomeIntegrationTests {
- // ...
- }
@Autowired
private WebApplicationContext wac;
这个注解无法识别,报错显示没有确定的bean,发现少加了@WebIntegrationTest注解,让Spring识别我正在测试一个web应用程序,所以会报错,加上这个注解就可以了。
刚刚接触Spring Boot,要多看官方的文档,了解原理,多看一些例子