【记录】用JUnit 4的@Test注解时报错java.lang.NullPointerException的原因与解决方法

项目场景:

在练习黑马点评的逻辑过期解决缓存击穿时,编写了一个预热缓存数据的单元测试

java 复制代码
@SpringBootTest
public class HmDianPingApplicationTests {

    @Resource
    private ShopServiceImpl shopService;
    @Test
    public void testSaveShop() throws InterruptedException {
        shopService.saveShop2Redis(1L,10L);
    }
    }

运行时报空指针异常

bash 复制代码
java.lang.NullPointerException: Cannot invoke "com.hmdp.service.impl.ShopServiceImpl.saveShop2Redis(java.lang.Long, java.lang.Long)" because "this.shopService" is null

原因分析:

这是由于选择的@Test注解选错了,应该选择org.junit.jupiter.api的那一个而不是org.junit的那一个。前者对应JUnit 5单元测试框架 后者对应JUnit 4单元测试框架。两者的对比如下:

JUnit 4: org.junit.Test

  • 用途 : JUnit 4 提供的基础单元测试注解,用于标记测试方法。

  • 特点: 不支持重复测试、参数化测试等高级功能(需要额外扩展)。

  • 测试方法必须是 public 的。(这也是为什么如果用了这个注解,测试方法没加public的话就没有启动运行的绿色箭头的原因)

  • 必须配合 JUnit 4 的测试运行器使用(如 @RunWith)。

JUnit 5: org.junit.jupiter.api.Test

  • 用途: JUnit 5(JUnit Jupiter)提供的现代化测试注解,是 JUnit 的全新版本。

  • 特点: 无需方法是public(包级权限或 protected 也可)。

  • 支持更多功能,如重复测试(@RepeatedTest)、参数化测试(@ParameterizedTest)。

    更强的扩展性,支持条件测试(如 @EnabledIf)。

  • 无需 @RunWith,改用 @ExtendWith 或直接支持 Spring的测试环境。

JUnit 4 中,Spring 和测试的整合依赖于 @RunWith(SpringRunner.class)(或较早的 SpringJUnit4ClassRunner)。

如果没有添加 @RunWith(SpringRunner.class) 注解,Spring 的测试上下文不会加载,@SpringBootTest 的功能无法生效。

结果是,@Resource(或 @Autowired)的依赖注入不会生效,导致 shopService 为 null。

JUnit 5 使用 SpringExtension(通过 @ExtendWith(SpringExtension.class) 内部实现)来加载 Spring 测试上下文。

@SpringBootTest 会自动应用 SpringExtension,所以无需显式声明 @ExtendWith,Spring 上下文会被正确加载。

因此,在 JUnit 4 中,如果你遗漏了 @RunWith(SpringRunner.class),Spring 的功能不会生效,而在 JUnit 5 中,这一步是自动完成的。

所以为什么 shopService 是 null呢?

bash 复制代码
shopService 的注入依赖于 Spring 容器。如果 Spring 测试上下文没有正确加载:
@Resource 或 @Autowired 不会生效。
导致 shopService 没有被注入,值为 null。
调用 shopService.saveShop2Redis(...) 时自然会抛出 NullPointerException。
在 JUnit 4 中,如果没有正确使用 @RunWith(SpringRunner.class):
Spring 测试上下文未加载,shopService 不会被注入。
这也是为什么用 JUnit 4 会抛出 NullPointerException,而 JUnit 5 不会的根本原因。

解决方案:

方法一 :修复JUnit 4的问题 ,在使用时添加@RunWith(SpringRunner.class)

java 复制代码
@RunWith(SpringRunner.class) // 启用 Spring 的测试功能
@SpringBootTest
public class HmDianPingApplicationTests {

    @Resource
    private ShopServiceImpl shopService;

    @Test
    public void testSaveShop() {
        shopService.saveShop2Redis(1L, 10L);
    }
}

方法二:优先选用JUnit 5的Test注解

相关推荐
Lao A(zhou liang)的菜园1 小时前
Oracle数据仓库在医院的应用场景
数据库·数据仓库·oracle
ac.char3 小时前
github.com/lib/pq 数据库链接完整示例方式
数据库·postgresql·golang
琢磨先生David5 小时前
责任链模式:构建灵活可扩展的请求处理体系(Java 实现详解)
java·设计模式·责任链模式
-曾牛6 小时前
使用Spring AI集成Perplexity AI实现智能对话(详细配置指南)
java·人工智能·后端·spring·llm·大模型应用·springai
Xiao Ling.6 小时前
设计模式学习笔记
java
MyikJ6 小时前
Java面试:从Spring Boot到分布式系统的技术探讨
java·大数据·spring boot·面试·分布式系统
louisgeek7 小时前
Java 插入排序之希尔排序
java
小兵张健7 小时前
用户、资金库表和架构设计
java·后端·架构
洛小豆7 小时前
ConcurrentHashMap.size() 为什么“不靠谱”?答案比你想的复杂
java·后端·面试
菠萝017 小时前
分布式CAP理论
数据库·c++·分布式·后端