question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MockKException: No other calls allowed in stdObjectAnswer than equals/hashCode/toString on trying to static mock Android TextUtils.isEmpty()

See original GitHub issue

Expected Behavior

I am trying to mock an interface method, which returns GoogleSignInOptions, where Android TextUtils are used internally.

Current Behavior

Statick mocking TextUtils causes MockK exception.

Context

  • MockK version: 1.10.0
  • OS: MacOS 10.15.6
  • Kotlin version: 1.4.10
  • JDK version: openjdk version "1.8.0_265
  • JUnit version: 5.6.2
  • Type of test: Unit test

Stack trace

Caused by: io.mockk.MockKException: No other calls allowed in stdObjectAnswer than equals/hashCode/toString
	at io.mockk.impl.stub.MockKStub.stdObjectAnswer(MockKStub.kt:78)
	at io.mockk.impl.recording.states.SafeLoggingState.call(SafeLoggingState.kt:9)
	at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
	at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
	at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
	at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
	at android.text.TextUtils.isEmpty(TextUtils.java)
	at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(com.google.android.gms:play-services-basement@@17.1.1:7)
	at com.google.android.gms.common.api.Scope.<init>(com.google.android.gms:play-services-basement@@17.1.1:2)
	at com.google.android.gms.common.api.Scope.<init>(com.google.android.gms:play-services-basement@@17.1.1:6)
	at com.google.android.gms.auth.api.signin.GoogleSignInOptions.<clinit>(com.google.android.gms:play-services-base@@17.1.0:151)
	... 97 more

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------

classpath 'com.google.gms:google-services:4.3.3'

dependencies {
       testImplementation "io.mockk:mockk:1.10.0"
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------

interface AuthService {
    fun getGoogleSignInOptions(): GoogleSignInOptions
}

class SomeViewModel(
    private val authService: AuthService
) : ViewModel() {

    fun getGoogleSignInOptions(): GoogleSignInOptions = authService.getGoogleSignInOptions()
}


class MockingTextUtilsInGoogleSignInOptionsTest {

    private val authService = mockk<AuthService>().apply {
        every { getGoogleSignInOptions() } returns mockk()
    }
    private val vm = SomeViewModel(authService)

    @Test
    fun getGoogleSignInOptions() {
        mockkStatic(TextUtils::class)
        every { TextUtils.isEmpty(any()) } returns false

        vm.getGoogleSignInOptions()
        verify(atMost = 1) { authService.getGoogleSignInOptions() }
    }
}

// -----------------------[ YOUR CODE ENDS HERE ] -----------------------

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:7

github_iconTop GitHub Comments

42reactions
om-qustodiocommented, Apr 7, 2021

This is still occurring.

1reaction
p-brilcommented, Oct 25, 2022

I have same issue(((

Read more comments on GitHub >

github_iconTop Results From Across the Web

MockKException: No other calls allowed in stdObjectAnswer ...
MockKException : No other calls allowed in stdObjectAnswer than equals/hashCode/toString on trying to static mock Android TextUtils.isEmpty()
Read more >
Method isEmpty in android.text.TextUtils not mocked - Stack ...
In your src/test/java folder, add android.text package and create TextUtils.java public class TextUtils { public static boolean ...
Read more >
Mocking Static Methods in Android: Let's Wrap it up
As the documentation explains, any dependency on Android code must be mocked. A quick example of a simple unit test:.
Read more >
Static method call not being mocked on some threads using ...
Hello everyone. I am trying to unit test some code that is using RxJava for asynchronicity and parallelism and that is using Realm...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found