Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot mock Color.argb(): every/verify {} block were run several times #801

Open
3 tasks done
tagy opened this issue Mar 14, 2022 · 2 comments
Open
3 tasks done

Cannot mock Color.argb(): every/verify {} block were run several times #801

tagy opened this issue Mar 14, 2022 · 2 comments
Labels

Comments

@tagy
Copy link

tagy commented Mar 14, 2022

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

To be able to mock Color.argb()

Current Behavior

The following unit test code results in the stacktrace blow:

mockkStatic(Color::class);
every { Color.argb(20, 30, 40, 50) } returns Color.GRAY
val color = Color.argb(20, 30, 40, 50)
assertThat(color).isEqualTo(Color.GRAY)

However the following codes works as expected, and the assertion passes:

mockkStatic(Color::class)
Color.argb(0, 0, 0, 0)
every { Color.argb(20, 30, 40, 50) } returns Color.GRAY
val color = Color.argb(20, 30, 40, 50)
assertThat(color).isEqualTo(Color.GRAY)

The only difference is the call to Color.argb between mockkStatic and every

Im confused why the first code does not work?

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version:1.12.3
  • OS: Mac OS 12.2.1
  • Kotlin version: 1.6.10
  • JDK version: 11.0.11
  • JUnit version: 4.13.2
  • Type of test: unit test

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
every/verify {} block were run several times. Recorded calls count differ between runs
Round 1: class android.graphics.Color.__staticInitializer__(), class android.graphics.Color.argb(20, 30, 40, 50)
Round 2: class android.graphics.Color.argb(20, 30, 40, 50)
io.mockk.MockKException: every/verify {} block were run several times. Recorded calls count differ between runs
Round 1: class android.graphics.Color.__staticInitializer__(), class android.graphics.Color.argb(20, 30, 40, 50)
Round 2: class android.graphics.Color.argb(20, 30, 40, 50)
	at io.mockk.impl.recording.SignatureMatcherDetector.detect$checkAllSameNumberOfCalls(SignatureMatcherDetector.kt:25)
	at io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:86)
	at io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:39)
	at io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:31)
	at io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:50)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:63)
	at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalEvery(API.kt:93)
	at io.mockk.MockKKt.every(MockK.kt:98)
	at com.avius.kiosk.ExampleUnitTest.exampleMockkTest(ExampleUnitTest.kt:33)
	at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@11.0.11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base@11.0.11/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@11.0.11/java.lang.reflect.Method.invoke(Method.java:566)
	at app//org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at app//org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at app//org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at app//org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at app//org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at app//org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:591)
	at app//org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:274)
	at app//org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:88)
	at java.base@11.0.11/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base@11.0.11/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base@11.0.11/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base@11.0.11/java.lang.Thread.run(Thread.java:829)

// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'androidx.test:runner:1.4.0'
    testImplementation 'androidx.test:core-ktx:1.4.0'
    testImplementation 'androidx.test.ext:truth:1.4.0'
    testImplementation 'org.robolectric:robolectric:4.7.3'
    testImplementation "io.mockk:mockk:1.12.3"
    testImplementation "io.mockk:mockk-agent-jvm:1.12.3"
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
package com.my.package

import android.graphics.Color
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.mockkStatic
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class ExampleUnitTest {

    @Test
    fun exampleUnitTest() {

        mockkStatic(Color::class)
        //Color.argb(0, 0, 0, 0)
        every { Color.argb(20, 30, 40, 50) } returns Color.GRAY
        val color = Color.argb(20, 30, 40, 50)
        assertThat(color).isEqualTo(Color.GRAY)
    }

}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
@Raibaz
Copy link
Collaborator

Raibaz commented Apr 5, 2022

Given that this is indeed a weird behavior, I don't really think you should be mocking the Color class, since it's basically a value object.

What is the behavior you are trying to test here?

@stale
Copy link

stale bot commented Jul 10, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.

@stale stale bot added the stale label Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants