Mockito JUnit5模拟公共接口时出现NullPointerException问题排查
Spring Boot + Mockito 5.x 构建时NullPointerException问题解决
在Spring Boot 2.7.7项目中使用Mockito 5.0.0和JUnit 5.6.2模拟公共接口时触发NullPointerException,测试用例在IntelliJ中可正常通过,但执行gradle clean build时失败,且项目中所有公共接口的模拟操作都存在该问题。
相关代码
被测试类
public class SendNotification { public NotificationProperty config; public PostDbClient postDbClient; public ObjectMapper objectMapper; public AmazonSNS snsClient; public NotificationParser notificationParser; }
测试类
@ExtendWith(MockitoExtension.class) public class SendNotificationTest { @Mock AmazonSNS amazonSNS; @Mock public NotificationProperty config; @Mock public PostDbClient postDbClient; @Mock public ObjectMapper objectMapper; @Mock public NotificationParser notificationParser; @InjectMocks SendNotification sendNotification; @BeforeEach public void createMocks() { // MockitoAnnotations.openMocks(this); } @Test public void shouldSendNotification() { System.out.println("Step1"); System.out.println(config); System.out.println("Step2"); System.out.println(postDbClient); System.out.println("Step3"); System.out.println(objectMapper); System.out.println("Step4"); System.out.println(notificationParser); System.out.println("Step5"); System.out.println(amazonSNS); System.out.println("Step6"); Mockito.when(amazonSNS.publish(any())).thenReturn(new PublishResult()); // doReturn(new PublishResult()).when(amazonSNS).publish(new PublishRequest()); } }
异常栈信息
java.lang.NullPointerException at java.base/java.lang.reflect.Method.getParameterTypes(Method.java:311) at org.mockito.internal.creation.DelegatingMethod.<init>(DelegatingMethod.java:20) at org.mockito.internal.invocation.DefaultInvocationFactory.createMockitoMethod(DefaultInvocationFactory.java:81) at org.mockito.internal.invocation.DefaultInvocationFactory.createInvocation(DefaultInvocationFactory.java:60) at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:83) at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:56) at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptSuperCallable(MockMethodInterceptor.java:145) at com.expedia.ers.singleview.glasscase.client.GlassCaseClientFactory$MockitoMock$KUPO0Qd7.toString(Unknown Source) at java.base/java.lang.String.valueOf(String.java:2951) at java.base/java.io.PrintStream.print(PrintStream.java:759) at org.gradle.internal.io.LinePerThreadBufferingOutputStream.println(LinePerThreadBufferingOutputStream.java:230)
测试控制台输出
Step1 config Step2 postDbClient Step3 objectMapper Step4 notificationParser Step5
Gradle依赖配置
springBootVersion = '2.7.7' testImplementation group: 'io.springfox', name: 'springfox-staticdocs', version: '2.6.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2' testImplementation 'org.mockito:mockito-inline:5.0.0' test { useJUnitPlatform() dependsOn cleanTest testLogging.showStandardStreams = true maxParallelForks = 1 }
问题分析与解决方案
核心原因
- 版本兼容性不匹配:Mockito 5.0.0引入了字节码处理的变更,与Spring Boot 2.7.x默认依赖的JUnit平台、字节码工具存在兼容性冲突,Gradle构建时的类加载机制与IntelliJ不同,导致Mock对象初始化失败。
- 依赖冲突:springfox-staticdocs 2.6.1依赖旧版本Mockito,与当前使用的Mockito 5.0.0产生冲突,干扰Mock对象创建流程。
- Gradle任务配置问题:
test任务中dependsOn cleanTest强制清理测试缓存,破坏了类加载的稳定性。
解决步骤
- 降级Mockito至兼容版本:Spring Boot 2.7.x官方推荐Mockito 4.x系列,修改依赖为:
testImplementation 'org.mockito:mockito-inline:4.11.0' - 排除冲突依赖:在springfox-staticdocs依赖中排除旧版Mockito:
testImplementation group: 'io.springfox', name: 'springfox-staticdocs', version: '2.6.1' { exclude group: 'org.mockito' } - 优化Gradle测试任务:移除
dependsOn cleanTest配置,避免不必要的缓存清理:test { useJUnitPlatform() testLogging.showStandardStreams = true maxParallelForks = 1 } - 简化测试初始化:确保测试类仅通过
@ExtendWith(MockitoExtension.class)完成Mock初始化,删除@BeforeEach中注释的冗余代码。 - 验证依赖树:执行
gradle dependencies查看testImplementation依赖树,确认无多版本Mockito或bytebuddy共存。
内容的提问来源于stack exchange,提问作者ankursingh1000
相关产品推荐
相关产品推荐

