1.依赖

依赖只需要导入Spring-Boot-starter、Spring-Boot-test(不需要另导入junit依赖)

  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
  </dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
//web里包含starter

2.测试包结构和源码包结构必须相同(踩了很大的坑才发现的!!!)

3. 注解

 注意点:

  • 如果要用到Spring容器里的Bean,要在类上加@SpringBootTest注解,不然会报空指针
  • 导的是org.junit.jupiter.api.Test,不是Junit的org.junit.Test 
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐