SpringBoot整合Junit单元测试(解决空指针异常)

1.依赖

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

XML 复制代码
  <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
相关推荐
用户3521802454751 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
昵称为空C2 天前
手撸一个动态 SQL 执行引擎:不重启服务,在线增删改查任意数据库
spring boot·后端
霸道流氓气质2 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
于先生吖3 天前
SpringBoot对接大模型开发AI命理测算系统:八字排盘与AI解析接口源码全解
人工智能·spring boot·后端
Flittly3 天前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
许彰午3 天前
39_Java单元测试JUnit入门
java·junit·单元测试
星落zx3 天前
Spring Boot 多模型集成:优雅调用全球主流大模型
人工智能·spring boot·chatgpt
一杯奶茶¥3 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
骇客之技术3 天前
AutoLua:在安卓上写 Lua 脚本
android·junit·lua