在 JUnit 5 中可以通过 @TestMethodOrder
和 @Order
自定义测试用例的执行顺序[1],但在 JUnit 4 中没有 @TestMethodOrder
注解,对应的是 @FixMethodOrder
注解,不过不如前者使用方便。
使用 Spring Initializr 创建 Spring Boot 项目时,默认是包含 spring-boot-starter-test 包的。
如果选择 spring-boot-starter-parent 2.3.10 版时,会发现其依赖的写法如下:
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
基于 spring-boot-start 2.2.1.RELEASE 写 Controller 的测试类时,出了好多异常的状况。特将正常运行的代码记录下来,以防遗忘。
依赖版本
使用 Maven 管理的依赖。
- JDK: 1.8
- parent:spring-boot-starter-parent 2.2.1.RELEASE
- dependencies:
- spring-boot-starter-web 2.2.1.RELEASE
- spring-boot-starter-test 2.2.1.RELEASE