07-SpringBoot自动化测试

nobility 发布于 2022-06-03 870 次阅读


SpringBoot自动化测试

SpringBoot整合Junit

使用官方提供的spring-boot-starter-test启动器(注意scope是test),就自动引入了Junit功能

若想在单元测试类中使用SpringBoot的功能,比如自动注入,就需要在该测试类上添加@SpringBootTest注解

JUnit常用注解

注解 描述
@Test 表示方法是测试方法,与Junit4不同,不声明任何属性
@Disabled 用于禁用测试类或测试方法,类似于Junit4的@Ignore
@RepeatedTest 表示方法是重复测试
@Timeout 方法执行的超时时间
@BeforeEach 表示该方法在每个测试方法执行前都执行
@AfterEach 表示该方法在每个测试方法执行后都执行
@BeforeAll 表示该方法在测试类的所有方法之前执行一次,只能用于static方法, 类似于Junit4的@BeforeClass
@AfterAll 表示该方法在测试类的所有方法之后执行一次,只能用于static方法, 类似于Junit4的@AfterClass

断言机制

静态引入Assertions包,即可直接使用断言方法,Junit5引入了Lambda的断言机制,可使用Executable接口进行编写测试用例

方法 说明
assertEquals(expected, actual[, message]) 判断两个对象或两个原始类型是否相等
assertNotEquals(expected, actual[, message]) 判断两个对象或两个原始类型是否不相等
assertArrayEquals(expected, actual[, message]) 判断两个数组内容是否相等
assertSame(expected, actual[, message]) 判断两个对象引用是否一致
assertNotSame(expected, actual[, message]) 判断两个对象引用是否不一致
assertTrue(expected[, message]) 判断给定的布尔值是否为true
assertFalse(expected[, message]) 判断给定的布尔值是否为false
assertNull(expected[, message]) 判断给定的对象引用是否为null
assertNotNull(expected[, message]) 判断给定的对象引用是否不为null
assertAll(Executable... executables) 所有Executable执行成功,则该断言成功
assertThrows(Class<T> expectedType, Executable executable) Executable抛出该异常,则该断言成功
assertTimeout(Duration timeout, Executable executable) Executable执行时间未超时,则该断言成功
fail(String message) 执行该方法该测试一定失败

假设机制

静态引入Assumptions包,即可直接使用假设方法

类似于断言机制,不同的是:断言失败则测试失败;假设失败则该测试终止并跳过

用于满足什么条件在进行测试的场景,方法与断言方法类似前缀改为assume即可

SpringBoot整合Swagger

  • Swagger2 2.x.x版本访问路径:http://IP:端口/swagger-ui.html
  • Swagger2 3.x.x版本访问路径:http://IP:端口/swagger-ui/index.html

Swagger用于生成API文档,已经API接口测试的工具

<dependency>  <!-- 添加swagger2相关功能 -->
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.x.x</version>
</dependency>
<dependency>  <!-- 添加swagger-ui相关功能 -->
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.x.x</version>
</dependency>

<!-- 添加swagger2 3.0.0只需要引入启动器即可 -->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>

自定义配置类,进行下面配置

@Configuration
@EnableSwagger2 // 启用Swagger2功能
public class Swagger2Config {
  @Bean
  public Docket createRestApi() {
    ApiInfo apiInfo = new ApiInfoBuilder()
        .title("API文档标题") //标题
        .description("API文档简单的描述信息") //描述
        .contact(new Contact("作者名",  //作者名
            "http://www.baidu.com",  //作者地址
            "admin@qq.com"))  //作者邮箱
        .version("1.0") // 版本
        .build();
    Docket docket = new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo)
        .select()
        .apis(RequestHandlerSelectors.basePackage("com"))  //com包下所有API都交给Swagger2管理
        .paths(PathSelectors.any())  //也可使用PathSelectors.regex()方法,正则进行URL匹配路径生成API文档
        .build();
    return docket;
  }
}

常用注解如下:

  • @Api:作用于控制器类上
    • value:简单描述
    • tags:所属分组,多个控制器类可共用一个tag
  • @ApiOperation :作用于接收Http请求的方法上
    • value :简单描述
    • notes :详细描述
    • tags:所属分组,不同控制器类的方法可共用一个tag
  • @ApiParam :作用于接收Http请求方法的参数上
    • name:参数名
    • value:参数描述
    • required:是否为必须参数
    • defaultValue:参数的默认值
  • @ApiModel :作用于实体类上
    • value:实体类名称,简单描述
    • description:详细描述
  • @ApiModelProperty :作用于实体类的属性上
    • name:字段名
    • value:字段描述
    • required:是否为必须参数
    • allowEmptyValue:是否允许为null
  • @ApiIgnore:忽略,可作用于类,方法,属性上
@Api(tags = "学生分组")
@RestController
public class Controller {
  @ApiOperation(value = "获取学生", notes = "根据请求学生id获取学生")
  @GetMapping("/swagger/student")
  public Student getStudent(
      @ApiParam(value = "学生ID", required = true, defaultValue = "1000")
      @RequestParam("id") String id
  ) {
    Student student = new Student();
    student.setId(id);
    student.setName("zhangsan");
    return student;
  }
}

@ApiModel(value = "学生实体类简单描述")
class Student {
  @ApiModelProperty("学生ID")
  private String id;
  @ApiModelProperty("学生名")
  private String name;
  /*setter和getter省*/
}
此作者没有提供个人介绍
最后更新于 2022-06-03