43. 测试

    Spring Boot提供许多工具和注解来帮助你测试你的应用。测试支持由2个模块提供:spring-boot-test包括核心条目(item),spring-boot-test-autoconfigure支持测试的自动配置。

    大部分开发者使用spring-boot-starter-test“Starter”,它导入Spring Boot test模块和Junit,AssertJ,Hamcrest,和许多别的有用的库。

    43.1 测试范围依赖

    spring-boot-starter-test“Starter”(在testscope内)包含下面提供的库:

    我们通常发现这些公共库当测试时很有用。如果这些库不适合你的需要,你可以添加你自己的额外的测试依赖。

    43.2 测试Spring Boot 应用

    依赖注入的一个主要优势是它使你的代码易于测试。你可以实例化对象通过使用new操作符甚至不用牵涉Spring。你也可以用模拟对象(mock objects)代替真实的依赖。

    通常地,……

    43.3 测试Spring Boot应用

    一个Spring Boot应用是一个Spring ApplicationContext,因此没什么非常特别的事要做才能测试beyond what you would normally do with a vanilla Spring context。

    只有你使用SpringApplication创建它时,外部属性,日志,和Spring Boot的其他特性,默认安装在该上下文。

    Spring Boot提供一个@SpringBootTest注解,这可以被用来作为一个可选项对于标准的spring-test@ContextConfiguration注解,当你需要使用Spring Boot特性时。该注解通过创建在你的测试中通过SpringApplication使用的ApplicationContext 来工作。除了@SpringBootTest,许多别的注解也被提供来测试一个应用的更多具体部分

    不要忘记为你的测试添加@RunWith(SpringRunner.class),否则那个注解会被忽略。 你可以使用@SpringBootTestwebEnvironment特性来进一步改善你的测试怎样运行:

    • MOCK
    • RANDOM_PORT
    • DEFINED_PORT
    • NONE

    如果你的测试是@Transactional注解的,默认地它在每个测试方法结束时回滚事务。但是,由于使用这个安排和RANDOM_PORT或者DEFINED_PORT暗示提供一个真实的servlet环境,HTTP客户端和服务端运行在不同的(separate)进程,也因此在分离的事务中。这种情况下,任何该服务器上发起的(initiated )事务都不会回滚。

    ……

    43.4 使用运行的服务器测试

    如果你需要启动一个完全运行地服务器来测试,我们推荐你使用随机端口。如果你使用@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT),每次你的测试运行时一个可用的随机端口会被使用。 @LocalServerPort注解可以被用来注入你的测试使用的实际的端口。方便起见,需要发起REST调用到启动的服务器的测试用例可以额外使用@Autowire注解一个WebTestClient,它处理到运行的服务器的相关连接,并comes with一个为不同的response专用的API,如下所示:

    1. import org.junit.Test;
    2. import org.junit.runner.RunWith;
    3.  
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
    7. import org.springframework.test.context.junit4.SpringRunner;
    8. import org.springframework.test.web.reactive.server.WebTestClient;
    9.  
    10. @RunWith(SpringRunner.class)
    11. @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
    12. public class RandomPortWebTestClientExampleTests {
    13.  
    14. @Autowired
    15. private WebTestClient webClient;
    16.  
    17. @Test
    18. public void exampleTest() {
    19. this.webClient.get().uri("/").exchange().expectStatus().isOk()
    20. .expectBody(String.class).isEqualTo("Hello World");
    21. }
    22.  
    23. }

    Spring Boot也提供一个TestRestTemplate能力(facility):

    1. import org.junit.Test;
    2. import org.junit.runner.RunWith;
    3.  
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
    7. import org.springframework.boot.test.web.client.TestRestTemplate;
    8. import org.springframework.test.context.junit4.SpringRunner;
    9.  
    10. import static org.assertj.core.api.Assertions.assertThat;
    11.  
    12. @RunWith(SpringRunner.class)
    13. @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
    14. public class RandomPortTestRestTemplateExampleTests {
    15.  
    16. @Autowired
    17. private TestRestTemplate restTemplate;
    18.  
    19. @Test
    20. public void exampleTest() {
    21. String body = this.restTemplate.getForObject("/", String.class);
    22. assertThat(body).isEqualTo("Hello World");
    23. }
    24.  
    25. }