You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android:Mock GoogleSignIn.getClient时触发NullPointerException问题排查

解决方法

1. 修复Mockito版本冲突

当前mockito-core(4.5.1)和mockito-inline(3.5.6)版本不一致,会导致静态Mock逻辑失效。将两者版本统一:

testImplementation 'org.mockito:mockito-core:4.5.1'
testImplementation 'org.mockito:mockito-inline:4.5.1'

2. 明确参数类型匹配

原测试中使用无类型的any()可能无法精准匹配GoogleSignIn.getClient的参数,改为指定具体类型的匹配器:

ms.`when` { 
    GoogleSignIn.getClient(
        any(Context::class.java), 
        any(GoogleSignInOptions::class.java)
    ) 
}.thenReturn(googleSignInClient)

3. 简化Mock初始化(可选)

利用@Mock注解的自动初始化功能,替换手动创建Mock对象的代码,减少冗余:

@Before
fun setup() {
    MockitoAnnotations.openMocks(this)
}

4. 添加调用验证(可选)

在测试末尾添加验证逻辑,确认静态方法确实被调用,便于排查问题:

// 在account.initialize()之后添加
ms.verify { GoogleSignIn.getClient(context, any(GoogleSignInOptions::class.java)) }

内容的提问来源于stack exchange,提问作者rysv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:53:18