Android:Mocking GoogleSignIn.getClient 导致空指针异常。

huangapple go评论79阅读模式
英文:

Android: Mocking GoogleSignIn.getClient is resulting in Null Pointer Exception

问题

尝试模拟静态对象的GoogleSignIn.getClient()方法,但从initialize()方法中收到以下错误:

getClient(context, gso) must not be null java.lang.NullPointerException: getClient(context, gso) must not be null

这是被测试的类:

package com.example.hellogooglesignin

import android.content.Context
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions

data class UserAccount(val context: Context) {
    private lateinit var googleSignInClient: GoogleSignInClient

    fun initialize() {
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build()
        googleSignInClient = GoogleSignIn.getClient(context, gso)
    }
}

以及单元测试代码:

package com.example.hellogooglesignin

import android.content.Context
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.Mock
import org.mockito.Mockito

class GoogleSignInTest {
    @Mock
    private lateinit var googleSignInClient: GoogleSignInClient
    @Mock
    private lateinit var context: Context

    @Before
    fun setup() {
        googleSignInClient = Mockito.mock(GoogleSignInClient::class.java)
        context = Mockito.mock(Context::class.java)
    }

    @Test
    fun testInitialize() {
        Mockito.mockStatic(GoogleSignIn::class.java).use { ms ->
            ms.`when`<GoogleSignInClient> { GoogleSignIn.getClient(any(), any()) }.thenReturn(googleSignInClient)

            val account = UserAccount(context)
            account.initialize()
            assertEquals(1, 1)
        }
    }
}

返回的GoogleSignInClient的模拟对象不是空的:

Android:Mocking GoogleSignIn.getClient 导致空指针异常。

有什么想法吗?

使用的依赖库:

implementation 'com.google.android.gms:play-services-auth:20.5.0'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:4.5.1'
testImplementation 'org.mockito:mockito-inline:3.5.6'
英文:

Trying to mock static object GoogleSignIn.getClient() method but getting this error from the initialize() method:

getClient(context, gso) must not be null
java.lang.NullPointerException: getClient(context, gso) must not be null

Here is the class under test

package com.example.hellogooglesignin

import android.content.Context
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions

data class UserAccount(val context: Context) {
    private lateinit var googleSignInClient: GoogleSignInClient

    fun initialize() {
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build()
        googleSignInClient = GoogleSignIn.getClient(context, gso)
    }
}

And the unit test code

package com.example.hellogooglesignin

import android.content.Context
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.Mock
import org.mockito.Mockito

class GoogleSignInTest {
    @Mock
    private lateinit var googleSignInClient: GoogleSignInClient
    @Mock
    private lateinit var context: Context

    @Before
    fun setup() {
        googleSignInClient = Mockito.mock(GoogleSignInClient::class.java)
        context = Mockito.mock(Context::class.java)
    }

    @Test
    fun testInitialize() {
        Mockito.mockStatic(GoogleSignIn::class.java).use { ms -&gt;
            ms.`when`&lt;GoogleSignInClient&gt; { GoogleSignIn.getClient(any(), any()) }.thenReturn(googleSignInClient)

            val account = UserAccount(context)
            account.initialize()
            assertEquals(1, 1)
        }
    }
}

Returned mock object for GoogleSignInClient is not null:

Android:Mocking GoogleSignIn.getClient 导致空指针异常。

Any idea?

Using:

    implementation &#39;com.google.android.gms:play-services-auth:20.5.0&#39;

    testImplementation &#39;junit:junit:4.13.2&#39;
    testImplementation &#39;org.mockito:mockito-core:4.5.1&#39;
    testImplementation &#39;org.mockito:mockito-inline:3.5.6&#39;

答案1

得分: 0

通过替换

GoogleSignIn.getClient(any(), any())

GoogleSignIn.getClient(any(Context::class.java), any(GoogleSignInOptions::class.java))

已解决。

英文:

Solved by replacing

GoogleSignIn.getClient(any(), any())

with

GoogleSignIn.getClient(any(Context::class.java), any(GoogleSignInOptions::class.java)).

huangapple
  • 本文由 发表于 2023年6月13日 08:05:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定