11、SpringBoot整合Junit

SpringBoot2.X使用Junit5作为测试平台

  • 修改POM文件添加Test启动器
  • 编写测试代码

从官网下载的项目和用idea教授叫工具创建的项目默认都有添加junit依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
   <!--junit-vintage-engine提供了Junit3与Junit4的运行平台-->
   <exclusions>
      <exclusion>
         <groupId>org.junit.vintage</groupId>
         <artifactId>junit-vintage-engine</artifactId>
      </exclusion>
   </exclusions>
</dependency>

当使用exclusion进行排除则该项目不支持Junit3和Junit4,如果使用就会报错,如下:

因为SpringBoot推荐用junit5进行单元测试,SpringBoot给我默认提供测试类

//@SpringBootTest默认回去找启动类,默认可以不配置,当项目有多个启动类的时候才需要配置
@SpringBootTest
class SpringbootexceptionandjunitApplicationTests {

   @Test
   void contextLoads() {
   }

}
原文地址:https://www.cnblogs.com/Ryuichi/p/13456861.html