SpringBoot项目中的测试类,无法注入类,注入类为空

开发中,需要用到测试类来测试接口

我最开始使用的注入方式是@Autowired,但是在执行测试时,注入类报空指针异常,一直为null

后来上网查到几种解决方案,最终在通过下述内容解决了:

1.测试类中添加注解

@SpringBootTest(classes = 项目启动类.class)

项目启动类对应的是Application类

2.使用@Autowired的同时,再手动new一次

相关推荐
短剑重铸之日1 天前
《SpringBoot4.0初识》第四篇:原生镜像
java·原生镜像·springboot4.0
程序员欣宸1 天前
LangChain4j实战之十二:结构化输出之三,json模式
java·人工智能·ai·json·langchain4j
superman超哥1 天前
Rust 范围模式(Range Patterns):边界检查的优雅表达
开发语言·后端·rust·编程语言·rust范围模式·range patterns·边界检查
天若有情6731 天前
打破思维定式!C++参数设计新范式:让结构体替代传统参数列表
java·开发语言·c++
亲爱的非洲野猪1 天前
从ReentrantLock到AQS:深入解析Java并发锁的实现哲学
java·开发语言
wheelmouse77881 天前
如何设置VSCode打开文件Tab页签换行
java·python
yangminlei1 天前
Spring Boot——日志介绍和配置
java·spring boot
云上凯歌1 天前
02 Spring Boot企业级配置详解
android·spring boot·后端
廋到被风吹走1 天前
【Spring】Spring Boot Starter设计:公司级监控SDK实战指南
java·spring boot·spring
码头整点薯条1 天前
启动报错:Invalid value type for attribute ‘factoryBeanObjectType‘ 解决方案
java