zoukankan      html  css  js  c++  java
  • Spring Boot 单元测试详解+实战教程

    Spring Boot 的测试类库

    Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块。

    • spring-boot-test:支持测试的核心内容。

    • spring-boot-test-autoconfigure:支持测试的自动化配置。

    开发进行只要使用 spring-boot-starter-test 启动器就能引入这些 Spring Boot 测试模块,还能引入一些像 JUnit,AssertJ,Hamcrest 及其他一些有用的类库,具体如下所示。

    • JUnit:Java 应用程序单元测试标准类库。

    • Spring Test & Spring Boot Test:Spring Boot 应用程序功能集成化测试支持。

    • AssertJ:一个轻量级的断言类库。

    • Hamcrest:一个对象匹配器类库。

    • Mockito:一个Java Mock测试框架,默认支付 1.x,可以修改为 2.x。

    • JSONassert:一个用于JSON的断言库。

    • JsonPath:一个JSON操作类库。

    下面是 Maven 的依赖关系图。

    以上这些都是 Spring Boot 提供的一些比较常用的测试类库,如果上面的还不能满足你的需要,你也可以随意添加其他的以上没有的类库。

    测试 Spring Boot 应用程序

    添加 Maven 依赖

    
     
    1. <dependency>

    2.    <groupId>org.springframework.boot</groupId>

    3.    <artifactId>spring-boot-starter-test</artifactId>

    4.    <version>1.5.10.RELEASE</version>

    5.    <scope>test</scope>

    6. </dependency>

    1、 要让一个普通类变成一个单元测试类只需要在类名上加入 @SpringBootTest 和 @RunWith(SpringRunner.class) 两个注释即可。

    2、 在测试方法上加上 @Test 注释。

    如果测试需要做 REST 调用,可以 @Autowire 一个 TestRestTemplate。

    
     
    1. @RunWith(SpringRunner.class)

    2. @SpringBootTest

    3. public class BBTestAA {

    4.   @Autowired

    5.   private TestRestTemplate testRestTemplate;

    6.   @Test

    7.   public void testDemo() {

    8.    ...

    9.   }

    10. }

    GET请求测试

    
     
    1. @Test

    2. public void get() throws Exception {

    3.    Map<String,String> multiValueMap = new HashMap<>();

    4.    multiValueMap.put("username","Java技术栈");

    5.    ActResult result = testRestTemplate.getForObject("/test/getUser?username={username}",ActResult.class,multiValueMap);

    6.    Assert.assertEquals(result.getCode(),0);

    7. }

    POST请求测试

    
     
    1. @Test

    2. public void post() throws Exception {

    3.    MultiValueMap multiValueMap = new LinkedMultiValueMap();

    4.    multiValueMap.add("username","Java技术栈");

    5.    ActResult result = testRestTemplate.postForObject("/test/post",multiValueMap,ActResult.class);

    6.    Assert.assertEquals(result.getCode(),0);

    7. }

    文件上传测试

    
     
    1. @Test

    2. public void upload() throws Exception {

    3.    Resource resource = new FileSystemResource("/home/javastack/test.jar");

    4.    MultiValueMap multiValueMap = new LinkedMultiValueMap();

    5.    multiValueMap.add("username","Java技术栈");

    6.    multiValueMap.add("files",resource);

    7.    ActResult result = testRestTemplate.postForObject("/test/upload",multiValueMap,ActResult.class);

    8.    Assert.assertEquals(result.getCode(),0);

    9. }

    文件下载测试

    
     
    1. @Test

    2. public void download() throws Exception {

    3.    HttpHeaders headers = new HttpHeaders();

    4.    headers.set("token","javastack");

    5.    HttpEntity formEntity = new HttpEntity(headers);

    6.    String[] urlVariables = new String[]{"admin"};

    7.    ResponseEntity<byte[]> response = testRestTemplate.exchange("/test/download?username={1}",HttpMethod.GET,formEntity,byte[].class,urlVariables);

    8.    if (response.getStatusCode() == HttpStatus.OK) {

    9.        Files.write(response.getBody(),new File("/home/javastack/test.jar"));

    10.    }

    11. }

  • 相关阅读:
    小程序对于华为Oppo的canvas二维码渲染数据量大
    SonarQube代码质量管理工具的升级(sonarqube6.2 + sonar-scanner-2.8 + MySQL5.6+)
    SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
    在try-catch机制优化IO流关闭时,OutputStreamWriter 数据流被截断
    Java中日期格式化SimpleDateFormat类包含时区的处理方法
    彻底删除mysql服务(清理注册表)
    PHP7新特性的介绍
    RESTful架构详解
    php-config 介绍
    用 phpize 编译共享 PECL 扩展库
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/9710170.html
Copyright © 2011-2022 走看看