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

@MockKExtension.CheckUnnecessaryStub annotation is applied to non-annotated classes #954

Closed
neubarth opened this issue Oct 24, 2022 · 5 comments

Comments

@neubarth
Copy link

Prerequisites

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

Expected Behavior

  • A test class T1 annotated with @MockKExtension.CheckUnnecessaryStub, which does not contain unnecessary stubbing, succeeds.
  • Another test class T2 without this annotation, and which contains unnecessary stubbing, also succeeds.
  • Both tests do not affect each other. CheckUnnecessaryStub is only applied to the annotated test class. The running order of tests does not matter.

Current Behavior

  • When running T1 and T2 in in this order, both tests succeed.
  • When running T2 and T1 in in this order, T1 fails due to the unnecessary stubbing in T2.

Steps to Reproduce

Create test file 1:

import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@MockKExtension.CheckUnnecessaryStub
class TestThatDoesNotAllowUnnecessaryStubbing {
    @MockK
    private lateinit var myObject: MyClass2

    @Test
    fun myTest() {
        every { myObject.toString() } returns "obj as string"
        Assertions.assertThat(myObject.toString()).isEqualTo("obj as string")
    }

    class MyClass2
}

Create test file 2:

import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class TestThatAllowsUnnecessaryStubbing {
    @MockK
    private lateinit var myObject: MyClass1

    @Test
    fun myTest() {
        every { myObject.toString() } returns "obj as string"
    }

    class MyClass1
}

When running both test files in this order (e.g. via IntelliJ or Gradle), both tests succeed.

When running file2 and then file1, then the test in file1 fails:

17:16:55.263 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for MyClass1 name=myObject#1
17:16:56.297 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for MyClass2 name=myObject#2
17:16:56.357 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering obj as string on MyClass2(myObject#2).toString()
17:16:56.647 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering obj as string on MyClass1(myObject#1).toString()

java.lang.AssertionError: Unnecessary stubbings detected.
Following stubbings are not used, either because there are unnecessary or because tested code doesn't call them :
1) obj as string.toString())

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.13.2
  • OS: Windows
  • Kotlin version: 1.6.10
  • JDK version: OpenJDK 17.0.4
  • JUnit version: 5.8.2
  • Type of test: unit test

Minimal reproducible code (the gist of this issue)

see above

@progvb
Copy link

progvb commented Nov 21, 2022

I have a similar problem with @MockKExtension.ConfirmVerification.

@And-smile
Copy link

Hi, have same issue, any progress on this?

@JeremiahDo
Copy link

I have a very similar issue. Somebody else has filed a bug for that here: #963

@progvb
Copy link

progvb commented Aug 28, 2023

Looks like it was fixed with #1099 and released in https://github.com/mockk/mockk/releases/tag/1.13.7

@neubarth
Copy link
Author

Great, thank you. I can confirm that it works now (version 1.13.9).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants